Originally this series was meant to be titled 'Any thing you can do I can do better', but to be honest after writing the Xml/XPath examples I realized the XLinq is really no better per say than Xml/XPath. However, what XLinq does bring to the table that Xml/XPath does not:
- Does not need to use the XPath query syntax
- Reads like English (mostly) when creating queries
- Lower barrier to entry for someone new to Xml (my opinion)
In Part 1 of this little mini series we will examine some common data access scenario's and how XLinq's syntax compares to that of standard Xml/XPath access.
Before I get started I will show you a sample of the XML document I am using.
<?xml version='1.0'?>
<root>
<system>
<list>
<subscribers>
<subscriber Type="Random">
<id>63425813</id>
<Email__Address>FakeAddress@comcast.net</Email__Address>
<EmailType>HTML</EmailType>
<Status>InActive</Status>
<First__Name>RAY</First__Name>
<Last__Name>Bob</Last__Name>
<PIN__Code>NaN</PIN__Code>
<PIPIN>33232</PIPIN>
<UID>418444</UID>
<ProgNum>0</ProgNum>
<Title />
<Username />
<Password />
</subscriber>
</subscribers>
</list>
<list>
<subscribers>
<subscriber>
<id>1271728821</id>
<Email__Address>FakeAddress@yahoo.com</Email__Address>
<EmailType>HTML</EmailType>
<Status>InActive</Status>
<First__Name>JOHN</First__Name>
<Last__Name>Foo</Last__Name>
<PIN__Code>NaN</PIN__Code>
<PIPIN>1254512</PIPIN>
<UID>1033488</UID>
<ProgNum />
<Title />
<Username />
<Password />
</subscriber>
</subscribers>
</list>
</system>
</root>
Example 1 - Simple data access
via Xml/XPath
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
// notice the @ is on the status -- this is how u search a element
Int32 listCount = xmlDocument.SelectNodes( "//list" ).Count;
Int32 subscribersCount = xmlDocument.SelectNodes( "//subscriber[@Type='Random']" ).Count;
via XLinq
XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();
Int32 listCount = xDocument.Descendants( "list" ).Count();
Int32 subscribersCount = xDocument.Descendants( "subscriber" ).Attributes( "Type" ).Where( e => e.Value == "Random" ).Count();
As you can see both examples are pretty light on the code and are pretty straight forward. This is one case where the new XLinq syntax is really no better/simpler then the Xml/XPath syntax. The issue with the XPath syntax is that
Example 2 - Get a list of nodes that match our search criteria
via Xml/Xpath
// notice the @ is missing on the status -- this is how u search a node
XmlDocument xmlDocument = XmlHelper.GetRawSnippetAsXmlDocument();
XmlNodeList xmlNodeList = xmlDocument.SelectNodes( "//subscribers/subscriber[Status='InActive']" );
foreach ( XmlElement element in xmlNodeList )
{
var id = element.SelectSingleNode( "//id" ).InnerText;
var type = element.Attributes[ "Type" ].Value;
}
via XLinq
XDocument xDocument = XmlHelper.GetRawSnippetAsXDocument();
var item = from doc in xDocument.Descendants( "subscriber" )
let status = (string)doc.Element( "Status" )
where status == "InActive"
select doc;
foreach ( XElement xElement in item )
{
var id = xElement.Element( "id" ).Value;
var type = xElement.Attribute( "Type" ).Value;
}
Now here is an example where I think the XLinq syntax is cleaner and easier to understand. There is no need to know or understand the XPath syntax, which can be a pain to learn. Also here you see how XLinq looks a lot like standard SQL syntax and should be familiar to most developers.
Well, above I have shown you a few simple examples that compare Xml/Path to XLinq, I hope you find this useful. The next post will compare how to update/add data to a document using the both Xml/XPath and XLinq.
Till next time,