Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 05-12-2008 5:15 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Xabatcha wrote re: XLinq'n in the new world - Data access comparisons with Xml/XPath
on 05-12-2008 9:09 AM

Did you have a chance to compare Xlinq how it handles big files? Let say bigger than 10MB or bigger.

UpComingCamera.Info » Blog Archive » XLinq'n in the new world - Data access comparisons with Xml/XPath wrote UpComingCamera.Info &raquo; Blog Archive &raquo; XLinq&#39;n in the new world - Data access comparisons with Xml/XPath
on 05-12-2008 9:13 AM

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

Matt Youngblut wrote re: XLinq'n in the new world - Data access comparisons with Xml/XPath
on 05-12-2008 9:14 AM

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.

Derik Whittaker wrote re: XLinq'n in the new world - Data access comparisons with Xml/XPath
on 05-12-2008 9:16 AM

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

Derik Whittaker wrote re: XLinq'n in the new world - Data access comparisons with Xml/XPath
on 05-12-2008 9:22 AM

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

Dew Drop - May 12, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - May 12, 2008 | Alvin Ashcraft's Morning Dew
on 05-12-2008 9:41 AM

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

gOODiDEA.NET wrote Interesting Finds: 2008.05.13
on 05-12-2008 8:30 PM

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

gOODiDEA wrote Interesting Finds: 2008.05.13
on 05-12-2008 8:31 PM

.NETVisualStudio2008SP1BetaVS2008SP1and.NETFXBetaPerformanceImprovementsIntroducing...

Reflective Perspective - Chris Alcock » The Morning Brew #92 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #92
on 05-13-2008 2:27 AM

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

Derik Whittaker wrote XLinq'n in the new world - Data manipulation comparisons with Xml/XPath
on 05-13-2008 6:37 AM

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

Derik Whittaker wrote XLinq'n in the new world - Structure Modification comparisons with Xml/XPath
on 05-14-2008 8:21 AM

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

Derik Whittaker wrote XLinq'n in the new world - Speed comparison with Xml/XPath
on 05-14-2008 11:02 AM

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

XPath - Common Mistake « Matt Youngblut’s Blog wrote XPath - Common Mistake &laquo; Matt Youngblut&#8217;s Blog
on 05-14-2008 4:45 PM

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)