Derik Whittaker



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
Using XLinq to manipulate an XML Document, XLinq U Rock

For the first time in a long time I needed to update values inside an XML document.  I have done this type of action many times in the past and have always hated it.  The syntax is clunky and the process is simply painful.

So today when I realized I need to do this, let me just tell you I was NOT looking forward to writing this code.  Then I remembered that with .Net 3.5 they added XLinq and it allows you to simply navigate and manipulate XML documents.  This was exactly what I was looking for.

In order to figure out how to use XLinq to manipulate a file I turned to the book Linq in Action to find the answer.  In the very short time I have had this book, I have found it to be very useful.

Ok, ok, enough chatter, lets get to the problem and how I solved it.

For my task, I need to open a XML Document that is stored as an embedded resource in our application, make changes to it and pass it along as an XML Document.  The end goal is to take this document and sent it along to one of our 3rd party vendors via the XML Api.

Here is the Raw XML that needed to be modified

<?xml version="1.0" encoding="utf-8" ?>

Below is the code needed to update this document with the provided values. 
In order to use XLinq you need to import System.Xml.Linq

    XmlDocument xmlDocument = METHOD TO LOAD XML DOCUMENT HERE

    // Convert to an XDoc so we can process this
    XDocument xDocument = XDocument.Parse( xmlDocument.OuterXml );

    XElement authorizationElement = xDocument.Element( root" ).Element( "authorization" );
    authorizationElement.SetElementValue( "username", "SomeValue" );
    authorizationElement.SetElementValue( "password", "SomeValue" );

    XElement systemElement = xDocument.Element( "root" ).Element( "call" );

    systemElement.SetElementValue( "call_name", "SomeValue" );
    systemElement.SetElementValue( "call_action", "SomeValue" );
    systemElement.SetElementValue( "search_type", "SomeValue" );
    systemElement.SetElementValue( "search_value", "SomeValue" );

    // convert the xDocument back to an XML document for later usage
    xmlDocument.LoadXml( xDocument.ToString() );
    return xmlDocument;

So as you can see I can very easily and very quickly manipulate this document with the correct values.  And because the syntax is in a fluent style it is VERY easy to read.

Till next time,

Posted 05-01-2008 4:26 PM by Derik Whittaker
Filed under: ,



Dew Drop - May 2, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - May 2, 2008 | Alvin Ashcraft's Morning Dew
on 05-02-2008 9:09 AM

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

Raman Basu wrote re: Using XLinq to manipulate an XML Document, XLinq U Rock
on 05-11-2008 11:29 PM

Good introduction but I expected more

Mike wrote re: Using XLinq to manipulate an XML Document, XLinq U Rock
on 09-08-2008 9:51 PM

Thank you very much!  It was just what I was looking for!!



About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)