Upload
valeriaramosg
View
229
Download
0
Embed Size (px)
Citation preview
8/13/2019 01+ +XML+Programming+in++.NET
1/29
Open XML Developer Workshop
XML Programming in .NET
8/13/2019 01+ +XML+Programming+in++.NET
2/29
Open XML Developer Workshop
Disclaimer
The information contained in this slide deck represents the current view of Microsoft Corporation on the issues discussed as of the date ofpublication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the
part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.This slide deck is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE
INFORMATION IN THIS DOCUMENT.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this slidedeck may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic,mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of MicrosoftCorporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in thisslide deck. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this slide deck does not giveyou any license to these patents, trademarks, copyrights, or other intellectual property.
Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events
depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo,person, place or event is intended or should be inferred.
2006 Microsoft Corporation. All rights reserved.
Microsoft, 2007 Microsoft Office System, .NET Framework 3.0, Visual Studio, and Windows Vista are either registered trademarks ortrademarks of Microsoft Corporation in the United States and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their respective owners.
8/13/2019 01+ +XML+Programming+in++.NET
3/29
Open XML Developer Workshop
Introduction to XML in .NETReading and Writing XML
Working with XML Namespaces
Validating XML
Querying XML
Overview
8/13/2019 01+ +XML+Programming+in++.NET
4/29
Open XML Developer Workshop
Widely used W3C standards for XML
W3C
XML 1.0
Namespaces
SchemaXPath
XSLT
8/13/2019 01+ +XML+Programming+in++.NET
5/29
Open XML Developer Workshop
Lesson: Reading and Writing XML
XML StreamsWorking with the DOM
8/13/2019 01+ +XML+Programming+in++.NET
6/29
Open XML Developer Workshop
Working with XML Streams
XmlReader
Here
Code
Your
Read()
ReadStartElement()ReadElementContentAsInt()
WriteStartElement()
WriteValue()WriteComment()
StreamFile
String
XmlWriter StreamFile
String
8/13/2019 01+ +XML+Programming+in++.NET
7/29Open XML Developer Workshop
The stream based approach
Reading XML withan XmlReader
XmlReaderSettingssettings = newXmlReaderSettings();settings.IgnoreComments = true;using(XmlReaderreader = XmlReader.Create(sample.xml , settings)){ while(reader.Read()){ if(reader.IsStartElement() && reader.LocalName == orderItem ){ reader.ReadToDescendant(quantity , xmlNamespace);intquantity = reader.ReadElementContentAsInt();
reader.ReadToNextSibling(unitPrice , xmlNamespace);decimalunitPrice = reader.ReadElementContentAsDecimal();Console.WriteLine(Total: + quantity * unitPrice);}}}
1034.99
8/13/2019 01+ +XML+Programming+in++.NET
8/29
Open XML Developer Workshop
The stream based approach
Writing XML with an XmlWriter
See Demos\01-XML Programming\Demo 1Stream based XML
XmlWriterSettingssettings = newXmlWriterSettings();settings.CloseOutput = true;settings.Indent = true;using(XmlWriterwriter = XmlWriter.Create(Console.Out, settings)){ writer.WriteStartDocument(true); // standalonewriter.WriteStartElement(order , xmlNamespace);writer.WriteStartElement(orderItem , xmlNamespace);writer.WriteElementString(quantity , xmlNamespace,XmlConvert.ToString(10));writer.WriteElementString(unitPrice , xmlNamespace,Convert.ToString(34.99));writer.WriteEndElement();writer.WriteEndElement();
}1034.99
8/13/2019 01+ +XML+Programming+in++.NET
9/29
Open XML Developer Workshop
Working with the DOM
DOM is the W3C programming interface for XMLDOM models an XML
source as a in memory
tree of nodes
You can use the DOM toNavigate and search
Modify content
Seattle
DocumentNode
ElementNode
ElementNode
AttributeNode
TextCharacterData
lcid=en-US
Seattle
8/13/2019 01+ +XML+Programming+in++.NET
10/29
Open XML Developer Workshop
Working with the DOMLoading from and saving to an XML source
The XmlDocument class is the core structureFor data store Use this method
String LoadXml()
Stream Load() and Save()
File
XmlReader / XmlWriter
XmlDocumentdocument = newXmlDocument();using(FileStream fs =newFileStream(sample.xml , FileMode.Open, FileAccess.Read)){document.Load(fs);}// save using a streaming writer with indentationusing(XmlTextWriterwriter = newXmlTextWriter(Console.Out)){ writer.Formatting = Formatting.Indented;document.Save(writer);}
8/13/2019 01+ +XML+Programming+in++.NET
11/29
Open XML Developer Workshop
Working with the DOMNavigating through XML
The XmlNode class serves as the base class for variousDOM elements
XmlElement and XmlAttribute are samples
?xmlversion=1.0?>XML.NET19.99
XmlNodebook = doc.FirstChild;XmlNodepriceNode = book.ChildNodes[1];XmlNodeisbnNode = book.GetAttributeNode(isbn );stringprice = priceNode.FirstChild.Value;stringisbn = isbnNode.Value;
8/13/2019 01+ +XML+Programming+in++.NET
12/29
Open XML Developer Workshop
Working with the DOMManipulating XML
The XmlDocument class serves as a factory for new XMLelements
19.99
// first create the nodesXmlDocumentdoc = newXmlDocument();XmlElementbookNode = doc.CreateElement(book );XmlNodepriceNode = doc.CreateElement(price );// set the node valuepriceNode.Value = 19.99 ;// set an attributebookNode.SetAttribute(title , XML is Cool );// add the nodes togetherbookNode.AppendChild(priceNode); doc.AppendChild(bookNode);
8/13/2019 01+ +XML+Programming+in++.NET
13/29
Open XML Developer Workshop
Working with the DOMManipulating XML
The XmlDocument allows creation of all DOM elements
To remove nodes, use the parent XmlNode
Importing XML from another XmlDocument
See Demos\01-XML Programming\Demo 2DOM based XML
XmlCommentcomment = doc.CreateComment(Some comment );XmlProcessingInstructionpi = doc.CreateProcessingInstruction(xml-stylesheet , type='text/xsl' href='style.xsl' );
bookNode.RemoveChild(priceNode); bookNode.RemoveAttribute(title );
XmlDocumentother;XmlNodenode = doc.ImportNode(other.DocumentElement, true);
8/13/2019 01+ +XML+Programming+in++.NET
14/29
Open XML Developer Workshop
Lesson: Working with XML namespaces
Basics of XML NamespacesThe XmlNamespaceManager class
8/13/2019 01+ +XML+Programming+in++.NET
15/29
Open XML Developer Workshop
Basics of XML namespaces
Many documents use the same element namesorderfor instance
To differentiate the elements you apply namespaces
http://mycompany.com/myOrderSchemabecomes
http://mycompany.com/myOrderSchema#order
Prefixes are used for abbreviation
xmlns:myO=http://mycompany.com/myOrderSchema
8/13/2019 01+ +XML+Programming+in++.NET
16/29
Open XML Developer Workshop
The XmlNamespaceManager class
The XmlNamespaceManager stores namespaces andprefixes
The prefixes in the source document do not have to be
equal to the one specified in the namespace manager
Create and use a namespace manager with all querieswhich use XML namespaces
XmlDocumentdocument;XmlNamespaceManagermgr = newXmlNamespaceManager();mgr.AddNamespace(myPf , http://xmlnamespaceInDocument );document.SelectSingleNode(//myPf:someElement , mgr);
8/13/2019 01+ +XML+Programming+in++.NET
17/29
Open XML Developer Workshop
Lesson: Validating XML
Overview of Validation Technologies
The Schema Object Model
Validating with the XmlReader
Validating an XmlDocument
The XmlSchemaValidator
8/13/2019 01+ +XML+Programming+in++.NET
18/29
Open XML Developer Workshop
Overview of Validation Technologies
Various XML validation technologies exist today:
Document Type Definitions (DTDs)
Xml-Data Reduced schema (XDRs)
XML-Schema Definition Language (XSDs)
Regular Language for XML Next Generation (Relax-NG)
XML-Schema is the standard validation technology today
Supported through the Schema Object Model
8/13/2019 01+ +XML+Programming+in++.NET
19/29
Open XML Developer Workshop
The Schema Object Model
Represented by the XmlSchema class
There are two collection classes for XmlSchema objects
The XmlSchemaCollection, which is now obsolete
The XmlSchemaSet class is the new/improved approach
The XmlSchemaSet
Improved standards compliance and performance
Forms one big logical schema from all contained itemsSupport for duplicate target namespaces
8/13/2019 01+ +XML+Programming+in++.NET
20/29
Open XML Developer Workshop
Validating with the XmlReader
Validation performed implicitly when configuring the
XmlReader.Create factory method
/ Create the XML SchemaXmlSchemaschema = null;using(XmlReaderreader = XmlReader.Create(sample.xsd )){schema = XmlSchema.Read(reader, null);}
// Create the settings for the XmlReaderXmlReaderSettingssettings = newXmlReaderSettings();settings.ValidationType = ValidationType.Schema;// add it to the XmlSchemaSet of the settings classsettings.Schemas.Add(schema); // Now read with the reader which also performs validationusing(XmlReaderreader = XmlReader.Create(sample.xml , settings)){ while(reader.Read()) ;}
8/13/2019 01+ +XML+Programming+in++.NET
21/29
Open XML Developer Workshop
Validating an XmlDocument
The XmlDocument acts as a container for XML schemas
See Demos\01-XML Programming\Demo 3Validating Documents
// Create the XMLSchema objectXmlSchemaschema = null;using(XmlReaderreader = XmlReader.Create(sample.xsd )){ schema = XmlSchema.Read(reader, null);}// Load a document and attach the schemaXmlDocumentdocument = newXmlDocument();document.Load(sample.xml );document.Schemas.Add(schema); // Validate it using the attached schemasdocument.Validate(OnValidateSchema);
8/13/2019 01+ +XML+Programming+in++.NET
22/29
Open XML Developer Workshop
Open XML schemas: best practice
The Ecma spec contains 89 separate schemas
There are circular references between those schemas
XmlSchemaSet can handle circular references
BUT only when theyre all in one Add() method call
The solution: use the provided all.xsdfile, which imports
all of the Open XML schemas into a single schema
HOL Solution\Solution\Schemas\all.xsd
8/13/2019 01+ +XML+Programming+in++.NET
23/29
Open XML Developer Workshop
Lesson: Querying XML
The Query Process
Querying the DOM
Working with XML Namespaces
8/13/2019 01+ +XML+Programming+in++.NET
24/29
Open XML Developer Workshop
The Query Process
MoveToRoot( )
MoveToNext( )
MoveToPrevious( )
MoveToFirstChild( )
MoveToParent( ) .ReturnType
XPathDocument
CreateNavigator
XPathNavigator
Evaluate( )
Select( )
Compile( )
Boolean
String
Double
NodeSet
XPathNodeIterator
MoveNext( )
For EachXPathExpression
5. Check the ReturnType1. Load XPathDocument
2. Create XPathNavigator 4. Compile XPathExpression
3. Create the Query
8/13/2019 01+ +XML+Programming+in++.NET
25/29
Open XML Developer Workshop
Querying the DOM
The XmlDocument has support for X-Path in various
places
Create a new XPathNavigator using the CreateNavigator()method
Use the XmlDocument SelectNodes(xpath)orSelectSingleNode(xpath)
Use an XPathDocument for optimized queries
XmlNodenode = document.SelectSingleNode(/order/orderItems );XPathNavigatornavigator = node.CreateNavigator();foreach(XPathNavigatoritemNavigator innavigator.Select(orderItem )){}
8/13/2019 01+ +XML+Programming+in++.NET
26/29
Open XML Developer Workshop
Automatic prefix mapping
All document contain their own prefix mappings, the
System.Xml 2.0 library allows you to use these in your
queries
Pass in an IXmlNamespaceResolver when using the
XPathNavigatorThe XML namespace needs to be in scope (i.e., you must
have read past it in the XPathNavigator):
XPathNavigatornavigator = document.CreateNavigator();navigator.MoveToChild(XPathNodeType.Element);foreach(XPathNavigatoritemNavigator innavigator.Select(o:orderItem , navigator)){}
8/13/2019 01+ +XML+Programming+in++.NET
27/29
Open XML Developer Workshop
Best practice: use IXmlNamespaceResolver
Sample syntax (from demo lab):
See Demos\01-XML Programming\Demo 4Querying Documents
XPathNavigatornavigator = document.CreateNavigator();XmlNamespaceManagernamespaceManager = newXmlNamespaceManager(navigator.NameTable);namespaceManager.AddNamespace(w ,http://schemas.openxmlformats.org/wordprocessingml/2006/main );XPathExpressionsearchPhrase =XPathExpression.Compile(@ //w:p[w:r/w:t[contains(text(), +toolStripTextBox1.Text + @ )]] , namespaceManager);
8/13/2019 01+ +XML+Programming+in++.NET
28/29
Open XML Developer Workshop
Introduction to XML in .NETReading and Writing XML
Working with XML Namespaces
Validating XML
Querying XML
Go through LAB 01XML Programming in .NET
Optional: demo labs 1-4
Review
8/13/2019 01+ +XML+Programming+in++.NET
29/29
Open XML Developer Workshop