Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



XLinq'n in the new world - Data access comparisons with Xml/XPath

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,



Comments

UpComingCamera.Info » Blog Archive » XLinq'n in the new world - Data access comparisons with Xml/XPath said:

Pingback from  UpComingCamera.Info  &raquo; Blog Archive   &raquo; XLinq&#39;n in the new world - Data access comparisons with Xml/XPath

# May 12, 2008 9:13 AM

Matt Youngblut said:

Derek - Is there a reason that you use //list and //subscribers in your XPath examples?  Unless you have no idea where those elements live within the document, you should be using /list and /subscribers (single / instead of double /) and defining the path.  // is very inefficient b/c it searches throughout the entire document looking for a list/subscribers element.  It looks like your XML has those elements in fixed locations.

# May 12, 2008 9:14 AM

Derik Whittaker said:

@Xabatcha

No, but that is a good idea.  I will do a post in this series that compares the speed differences between the 2 on various size files.

# May 12, 2008 9:16 AM

Derik Whittaker said:

@MATT

The only reason i guess is lack of in depth knowledge of XPath.  Which actually goes to the point of these posts.  Having to have in depth knowledge of XPath can lead to issues if you DO NOT.

# May 12, 2008 9:22 AM

Dew Drop - May 12, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - May 12, 2008 | Alvin Ashcraft's Morning Dew

# May 12, 2008 9:41 AM

gOODiDEA.NET said:

.NET Visual Studio 2008 SP1 Beta VS2008 SP1 and .NET FX Beta Performance Improvements Introducing JScript

# May 12, 2008 8:30 PM

gOODiDEA said:

.NETVisualStudio2008SP1BetaVS2008SP1and.NETFXBetaPerformanceImprovementsIntroducing...

# May 12, 2008 8:31 PM

Reflective Perspective - Chris Alcock » The Morning Brew #92 said:

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #92

# May 13, 2008 2:27 AM

Derik Whittaker said:

Originally this series was meant to be titled &#39;Any thing you can do I can do better&#39;, but to

# May 13, 2008 6:37 AM

Derik Whittaker said:

Originally this series was meant to be titled &#39;Any thing you can do I can do better&#39;, but to

# May 14, 2008 8:21 AM

Derik Whittaker said:

Originally this series was meant to be titled &#39;Any thing you can do I can do better&#39;, but to

# May 14, 2008 11:02 AM

XPath - Common Mistake « Matt Youngblut’s Blog said:

Pingback from  XPath - Common Mistake &laquo; Matt Youngblut&#8217;s Blog

# May 14, 2008 4:45 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!