29
Open XML Developer Workshop XML Programming in .NET

Open XML Developer Workshop XML Programming in.NET

Embed Size (px)

Citation preview

Page 1: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

XML Programming in .NETXML Programming in .NET

Page 2: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

DisclaimerDisclaimerThe information contained in this slide deck represents the current view of Microsoft Corporation on the issues discussed as of the date of

publication. 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 slide deck 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 Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this slide deck. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this slide deck does not give you 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 or

trademarks 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.

Page 3: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Introduction to XML in .NETReading and Writing XMLWorking with XML NamespacesValidating XMLQuerying XML

OverviewOverview

Page 4: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Widely used W3C standards for XMLWidely used W3C standards for XML

W3C

Page 5: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Lesson: Reading and Writing XMLLesson: Reading and Writing XML

XML StreamsWorking with the DOM

Page 6: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Working with XML StreamsWorking with XML Streams

XmlReaderHere

Code

Your

Read()ReadStartElement()ReadElementContentAsInt()

WriteStartElement()WriteValue()WriteComment()

StreamFile String

XmlWriterStreamFile String

Page 7: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

The stream based approachThe stream based approach

Reading XML with an XmlReader

XmlReaderSettings settings = new XmlReaderSettings();settings.IgnoreComments = true;

using (XmlReader reader = XmlReader.Create("sample.xml", settings)){ while (reader.Read()) { if (reader.IsStartElement() && reader.LocalName == "orderItem") { reader.ReadToDescendant("quantity", xmlNamespace); int quantity = reader.ReadElementContentAsInt();

reader.ReadToNextSibling("unitPrice", xmlNamespace); decimal unitPrice = reader.ReadElementContentAsDecimal(); Console.WriteLine("Total: " + quantity * unitPrice); } }}

<order> <orderItem> <quantity>10</quantity> <unitPrice>34.99</unitPrice> </orderItem></order>

Page 8: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

The stream based approachThe stream based approach

Writing XML with an XmlWriter

See Demos\01-XML Programming\Demo 1 – Stream based XML

XmlWriterSettings settings = new XmlWriterSettings();settings.CloseOutput = true;settings.Indent = true;using (XmlWriter writer = XmlWriter.Create(Console.Out, settings)){ writer.WriteStartDocument(true); // standalone writer.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();}

<order> <orderItem> <quantity>10</quantity> <unitPrice>34.99</unitPrice> </orderItem></order>

Page 9: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Working with the DOMWorking with the DOM

DOM is the W3C programming interface for XMLDOM models an XML source as a in memory tree of nodesYou can use the DOM to

Navigate and searchModify content

<city> <name lcid=“en-US”> Seattle </name></city>

DocumentNode

ElementNode

ElementNode

AttributeNode

TextCharacterData

<City>

<Name>

lcid=“en-US”

Seattle

Page 10: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Working with the DOMLoading from and saving to an XML sourceWorking 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

XmlDocument document = new XmlDocument();using (FileStream fs = new FileStream("sample.xml", FileMode.Open, FileAccess.Read)){ document.Load(fs);}// save using a streaming writer with indentationusing (XmlTextWriter writer = new XmlTextWriter(Console.Out)){ writer.Formatting = Formatting.Indented; document.Save(writer);}

Page 11: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Working with the DOMNavigating through XMLWorking with the DOMNavigating through XML

The XmlNode class serves as the base class for various DOM elements

XmlElement and XmlAttribute are samples

<?xml version="1.0"?><book isbn="123456789"> <title>XML.NET</title> <price>19.99</price></book>

XmlNode book = doc.FirstChild;XmlNode priceNode = book.ChildNodes[1];XmlNode isbnNode = book.GetAttributeNode("isbn");

string price = priceNode.FirstChild.Value;string isbn = isbnNode.Value;

Page 12: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Working with the DOMManipulating XMLWorking with the DOMManipulating XML

The XmlDocument class serves as a factory for new XML elements

<book title="XML is Cool"> <price>19.99</price></book>

// first create the nodesXmlDocument doc = new XmlDocument();XmlElement bookNode = doc.CreateElement("book");XmlNode priceNode = 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);

Page 13: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Working with the DOMManipulating XMLWorking 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 2 – DOM based XML

XmlComment comment = doc.CreateComment("Some comment");XmlProcessingInstruction pi = doc.CreateProcessingInstruction( "xml-stylesheet", "type='text/xsl' href='style.xsl'");

bookNode.RemoveChild(priceNode);bookNode.RemoveAttribute("title");

XmlDocument other;XmlNode node = doc.ImportNode(other.DocumentElement, true);

Page 14: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Lesson: Working with XML namespacesLesson: Working with XML namespaces

Basics of XML Namespaces The XmlNamespaceManager class

Page 15: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Basics of XML namespacesBasics of XML namespaces

Many documents use the same element names“order” for instance

To differentiate the elements you apply namespaceshttp://mycompany.com/myOrderSchemabecomes “http://mycompany.com/myOrderSchema#order”

Prefixes are used for abbreviationxmlns:myO=“http://mycompany.com/myOrderSchema”

<p:presentation xmlns:p="http://schemas.openxml.../presentationml/2006/main"> <p:sldMasterIdLst /></p:presentation>

Page 16: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

The XmlNamespaceManager classThe XmlNamespaceManager class

The XmlNamespaceManager stores namespaces and prefixesThe prefixes in the source document do not have to be equal to the one specified in the namespace managerCreate and use a namespace manager with all queries which use XML namespaces

XmlDocument document;XmlNamespaceManager mgr = new XmlNamespaceManager();mgr.AddNamespace("myPf", "http://xmlnamespaceInDocument");document.SelectSingleNode("//myPf:someElement", mgr);

Page 17: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Lesson: Validating XMLLesson: Validating XML

Overview of Validation TechnologiesThe Schema Object ModelValidating with the XmlReaderValidating an XmlDocumentThe XmlSchemaValidator

Page 18: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Overview of Validation TechnologiesOverview 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 todaySupported through the Schema Object Model

Page 19: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

The Schema Object ModelThe Schema Object Model

Represented by the XmlSchema class

There are two collection classes for XmlSchema objectsThe XmlSchemaCollection, which is now obsoleteThe XmlSchemaSet class is the new/improved approach

The XmlSchemaSetImproved standards compliance and performanceForms one big ‘logical’ schema from all contained itemsSupport for duplicate target namespaces

Page 20: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Validating with the XmlReaderValidating with the XmlReader

Validation performed implicitly when configuring the XmlReader.Create factory method

// Create the XML SchemaXmlSchema schema = null;using (XmlReader reader = XmlReader.Create("sample.xsd")){ schema = XmlSchema.Read(reader, null);}

// Create the settings for the XmlReaderXmlReaderSettings settings = new XmlReaderSettings();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 (XmlReader reader = XmlReader.Create("sample.xml", settings)){ while (reader.Read()) ;}

Page 21: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Validating an XmlDocumentValidating an XmlDocument

The XmlDocument acts as a container for XML schemas

See Demos\01-XML Programming\Demo 3 – Validating Documents

// Create the XMLSchema objectXmlSchema schema = null;using (XmlReader reader = XmlReader.Create("sample.xsd")){ schema = XmlSchema.Read(reader, null);}

// Load a document and attach the schemaXmlDocument document = new XmlDocument();document.Load("sample.xml");document.Schemas.Add(schema);

// Validate it using the attached schemasdocument.Validate(OnValidateSchema);

Page 22: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Open XML schemas: best practiceOpen XML schemas: best practice

The Ecma spec contains 89 separate schemasThere are circular references between those schemas

XmlSchemaSet can handle circular referencesBUT … only when they’re all in one Add() method call

The solution: use the provided all.xsd file, which imports all of the Open XML schemas into a single schema

HOL Solution\Solution\Schemas\all.xsd

Page 23: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Lesson: Querying XMLLesson: Querying XML

The Query ProcessQuerying the DOMWorking with XML Namespaces

Page 24: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

The Query ProcessThe 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

Page 25: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Querying the DOMQuerying the DOM

The XmlDocument has support for X-Path in various places

Create a new XPathNavigator using the CreateNavigator() methodUse the XmlDocument SelectNodes(xpath) or SelectSingleNode(xpath)

Use an XPathDocument for optimized queries

XmlNode node = document.SelectSingleNode("/order/orderItems");XPathNavigator navigator = node.CreateNavigator();foreach (XPathNavigator itemNavigator in navigator.Select("orderItem")){}

Page 26: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Automatic prefix mappingAutomatic prefix mapping

All document contain their own prefix mappings, the System.Xml 2.0 library allows you to use these in your queriesPass 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):

XPathNavigator navigator = document.CreateNavigator();navigator.MoveToChild(XPathNodeType.Element);foreach (XPathNavigator itemNavigator in navigator.Select("o:orderItem", navigator)){}

Page 27: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Best practice: use IXmlNamespaceResolverBest practice: use IXmlNamespaceResolver

Sample syntax (from demo lab):

See Demos\01-XML Programming\Demo 4 – Querying Documents

XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(navigator.NameTable);

namespaceManager.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");

XPathExpression searchPhrase = XPathExpression.Compile(@"//w:p[w:r/w:t[contains(text(),""" + toolStripTextBox1.Text + @""")]]", namespaceManager);

Page 28: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop

Introduction to XML in .NETReading and Writing XMLWorking with XML NamespacesValidating XMLQuerying XML

Go through LAB 01 – XML Programming in .NETOptional: demo labs 1-4

ReviewReview

Page 29: Open XML Developer Workshop XML Programming in.NET

Open XML Developer Workshop