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)
For Part 1 of this series check here
For Part 2 of this series check here
For Part 3 of this series check here
In Part 4 of this little mini-series we will examine the speed differences between XLinq and Xml/XPath.
Before I get started I will show you a sample of the XML document I am using. The sample document I am using today is 5.25mb in size.
<?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 - Loading a XML document from Resource Stream
There is no real code to show here, but I do have some speed numbers to chat about. I ran my test 2 times. The first time I loaded into a XLinq object first then into an XmlDocument object and for the second run, I swapped the order.
As it turns out the first document to load each time is slower, but you can get a look at the speed difference by looking at the delta between each load time.
Load Time
| XmlDocument | 515.00 | 327.72 | 187.27 (Delta) |
| XLinq | 358.94 | 546.2135 | 187.27 (Delta) |
By looking at the above, there is no real speed difference, which I expected. (Time above is in milliseconds)
Example 2 - Finding a Unique value in the Xml Document
For this example I decided to try to find a single node inside my larger xml file. In order to try to get good sample I ran each test 5 times and the times below are the average of all those runs
Run time (5 run avg)
| XLinq | 44.62 |
| Xml/XPath | 125.65 |
I have to say that I was kinda surprised by the results. I figured that the Xml/XPath way would be a little faster, but turns out it is 3 times slower. (Time above is in milliseconds). ** NOTE *** The times above do NOT include the time needed to load the XML document into the correct object model.
From the information in this mini-series i would conclude that if you need to do any type of XML reading/manipulation/creation I would make the jump over to using XLinq as your preferred way of doing so.
Till next time,