XPath XML data processing in Silverlight by Anton

In the latest Silverlight version you can manupulate XML data not just by XMLReader or XMLWriter objects, but with new XPath – the query language for XML.

XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C). The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. In popular use (though not in the official specification), an XPath expression is often referred to simply as an XPath.
Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSLT, subsets of the XPath query language are used in other W3C specifications such as XML Schema and XForms. Currently XPath has version 2.0. You can find full language specification at the Official W3C Site.

How can XPath help us:

Actually, this language is very familiar to the jQuery. It provides access to the different nodes of the XML tree.
You can:

  1. Select Nodes
  2. Predicates (to find a specific node or a node that contains a specific value)
  3. Select Unknown Nodes
  4. Select Several Paths
  5. Axes (they defines a node-set relative to the current node)
  6. Operators (for nodes comparing or expression evaluation)

XPath support was added to the Silverlight Toolkit since March,2010. You can just add System.Xml.XPath namespace to your project and use all needed functions.

First, we will need XDocument object. It contains the information necessary for a valid XML document. This includes an XML declaration, processing instructions, and comments.

Note that you only have to create XDocument objects if you require the specific functionality provided by the XDocument class. In many circumstances, you can work directly with XElement. Working directly with XElement is a simpler programming model.
XDocument derives from XContainer. Therefore, it can contain child nodes. However, XDocument objects can have only one child XElement node. This reflects the XML standard that there can be only one root element in an XML document.

If you will write something like this:
XDocument srcTree = new XDocument(
new XComment("This is a comment"),
new XElement("Root",
new XElement("Child1", "data1"),
new XElement("Child2", "data2"),
new XElement("Child3", "data3"),
new XElement("Child2", "data4"),
new XElement("Info5", "info5"),
new XElement("Info6", "info6"),
new XElement("Info7", "info7"),
new XElement("Info8", "info8")
)
);

You will automatically create a new XML file structure with 1 comment, 1 root directory and 8 nodes.
If you want to read XML – use XDocument.Load method. It take String, TextReader, XMLReader, Stream as input data source. Choose any of them.

XDocument doc = XDocument.Load("some.xml");

And you can parse String to get the structure of the document:

string str =
@"

Content
";
XDocument doc = XDocument.Parse(str);

You will get this as result:

Content


Also, you can call CreateReader method. You will get the standard XMLReader object with basic operations on the XML.

But what about expressions? There are 3 methods to use in case of expressions.

    • XPathEvaluate //Evaluates XPath expression
    • XPathSelectElement //Selects an XElement using a XPath expression
    • XPathSelectElements //Selects a collection of elements using an XPath expression.

We can use it like this:

IEnumerable att = (IEnumerable)xdoc.XPathEvaluate("/root/@a"); //Returns the a attribute of the root node

XElement el = xdoc.XPathSelectElement(“./name1”); //Returns the element with specific name

IEnumerable elements = xdoc.XPathSelectElements(XPathExpression); //This will return a collection of XElements found with expression

To test something right now go to XPathPad page. This application allow to select data from XML rely on XPAth expression. All sources available here.

More info you can find at:
XDocument MSDN,
Some XPath sample