MSDN Community - Windows UWP API Reference

Exploring the Data and XML APIs for Universal Windows Platform Applications

Windows.Data.Xml Namespace

This section provides an overview and detailed reference for the Windows.Data.Xml namespace, which enables applications to parse, create, and manipulate XML data.

Core Classes

XmlDocument

Represents an XML document. This is the primary class for working with XML data.

Public methods:
  • LoadXml(string xmlString): Loads an XML document from a string.
  • LoadXmlFromFile(string filePath): Loads an XML document from a file.
  • SaveToFile(string filePath): Saves the current XML document to a file.
  • SelectSingleNode(string xPath): Selects a single node using an XPath query.
  • SelectNodes(string xPath): Selects a collection of nodes using an XPath query.
Example: Loading and Querying XML
using Windows.Data.Xml.Dom;

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<root><item id='1'>Hello</item><item id='2'>World</item></root>");

XmlNodeList items = doc.SelectNodes("//item");
foreach (IXmlNode item in items)
{
    System.Diagnostics.Debug.WriteLine(item.InnerText);
}

XmlNodeList

Represents an ordered collection of IXmlNode objects.

Public properties:
  • Count: Gets the number of nodes in the list.
  • Item(uint index): Gets the IXmlNode at the specified index.

IXmlNode

Represents a node in the XML document tree. This is an interface implemented by various node types.

Common properties:
  • NodeType: Gets the type of the node (e.g., Element, Attribute, Text).
  • InnerText: Gets the concatenated text content of the node and all its descendants.
  • Attributes: Gets a collection of attribute nodes associated with an element node.
  • ChildNodes: Gets a collection of the child nodes of this node.

Common Node Types

The IXmlNode interface is implemented by several concrete types, each representing a different part of an XML document:

NodeType Enum Description Key Members
XmlNodeType.Element Represents an XML element. Name, Attributes, ChildNodes
XmlNodeType.Attribute Represents an attribute of an element. Name, Value
XmlNodeType.Text Represents the text content within an element. InnerText
XmlNodeType.Comment Represents an XML comment. InnerText
XmlNodeType.DocumentFragment Represents an empty node that can hold other nodes. ChildNodes

XPath Support

The Windows.Data.Xml.Dom namespace provides basic support for XPath queries through the SelectSingleNode and SelectNodes methods of XmlDocument.

Common XPath examples:

  • //elementName: Selects all elements with the given name anywhere in the document.
  • /root/elementName: Selects elements with the given name that are direct children of the root.
  • //element[@attribute='value']: Selects elements with a specific attribute value.
  • //element[1]: Selects the first occurrence of an element.

Working with Attributes

Attributes are accessed via the Attributes property of an IXmlNode (specifically when the node type is Element).

Example: Accessing and Modifying Attributes
using Windows.Data.Xml.Dom;

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<person name='Alice' age='30'/>");

XmlElement personElement = (XmlElement)doc.DocumentElement;

// Get attribute value
System.Diagnostics.Debug.WriteLine($"Name: {personElement.GetAttribute("name")}");

// Set attribute value
personElement.SetAttribute("age", "31");
personElement.SetAttribute("city", "New York");

System.Diagnostics.Debug.WriteLine(personElement.OuterXml);

Creating XML Nodes

You can programmatically create new XML elements, attributes, and text nodes.

Example: Creating and Appending Nodes
using Windows.Data.Xml.Dom;

XmlDocument doc = new XmlDocument();
XmlElement rootElement = doc.CreateElement("data");
doc.AppendChild(rootElement);

XmlElement newItem = doc.CreateElement("item");
newItem.SetAttribute("id", "unique123");

XmlText textNode = doc.CreateTextNode("This is a new item.");
newItem.AppendChild(textNode);

rootElement.AppendChild(newItem);

System.Diagnostics.Debug.WriteLine(doc.GetXml());

Error Handling

Loading invalid XML can throw exceptions (e.g., System.Xml.XmlException). It's good practice to wrap XML loading operations in try-catch blocks.

Example: Handling XML Parsing Errors
using Windows.Data.Xml.Dom;
using System.Xml;

XmlDocument doc = new XmlDocument();
try
{
    doc.LoadXml(@"<root><item>Valid XML</item></root>");
    // Process document
}
catch (XmlException ex)
{
    System.Diagnostics.Debug.WriteLine($"Error parsing XML: {ex.Message}");
}