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
In Part 2 of this little mini series we will examine some common data manipulation scenario's and how XLinq's syntax compares to that of standard Xml/XPath access. In general will we review how do update the content of both elements and attributes in an XML document.
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 - Updating Element Values
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']" );
// Attribute value before updates
//
//
foreach ( XmlElement element in xmlNodeList )
{
// this will only set the value. if the node does NOT exist you will get an exception.
element.SelectSingleNode( "//Status" ).InnerText = "Active";
}
// Attribute values afterupdate
//
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;
// Attribute value before updates
// InActive
foreach ( XElement xElement in item )
{
// this will set the value or create it if the element does not exist
xElement.SetElementValue( "Status", "Active" );
// Another option is to go after the element directly, but this requires a
// null check first
if ( xElement.Element( "Status" ) != null )
{
xElement.Element( "Status" ).SetValue( "Active" );
}
}
// Attribute values after update
// Active
As you can see the differences between using XLinq and Xml/XPath for updating elements is not the big. However, again you have to know/understand how to use XPath to find your nodes. There is ONE thing you need to be aware of though. When trying to update an element via XPath the element MUST exist. If it does not you will get a runtime exception. With XLinq there is a way to do this and not have to worry about that. To me that is pretty nice.
Example 2 - Updating Attribute Values
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']" );
// Attribute value before updates
//
foreach ( XmlElement element in xmlNodeList )
{
// this will set the value or create it if the attribute does not exist
element.SetAttribute( "Type", "SomeValue" );
}
// Attribute values afterupdate
//
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;
// Attribute value before updates
//
foreach ( XElement xElement in item )
{
// this will set the value or create it if the attribute does not exist
xElement.SetAttributeValue( "Type", "MyValue" );
// Another option is to go after the attribute directly, but this requires a
// null check first
if ( xElement.Attribute( "Type" ) != null )
{
xElement.Attribute( "Type" ).SetValue( "SomeValue" );
}
}
// Attribute values afterupdate
//
Above shows you that updating attributes are almost identical and in this case there is no need to know/understand XPath in order to select the single node you are trying to update.
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 modify the structure of a document using the both Xml/XPath and XLinq.
Till next time,
Posted
05-13-2008 5:37 AM
by
Derik Whittaker