Finding a Node in an XML String

Posted Sun, Jan 31 2010 14:21 by Deborah Kurata

A common requirement with an XML file is to find a particular node. If you are targeting the .NET framework 3.5 or higher, you can find the node using Linq to XML or by using Lambda expressions.

As with many of my prior XML examples, the XML string is as follows:

<States>
  <State name="Wisconsin">
    <Regions>
      <Region name="Milwaukee">
        <Area name="Mukwanago"/>
        <Area name="Germantown"/>
      </Region>
      <Region name="Fox Valley">
        <Area name="Oshkosh" />
        <Area name="Appleton" />
      </Region>    
    </Regions>
  </State>
</States>

The code to find the node for the Milwaukee region is as follows:

In C#:

// Be sure to set a reference to System.Core and System.Xml.Linq
XElement states  = XElement.Load("testXML.xml");

// Using LINQ
XElement foundNode;
var query = from XElement r in states.Descendants("Region")
                   where r.Attribute("name").Value == "Milwaukee"
                   select r;
foundNode = query.FirstOrDefault();

// Using Lambda expressions
foundNode = states.Descendants("Region").
     Where(r => r.Attribute("name").Value ==
                         "Milwaukee").FirstOrDefault();

In VB:

' Be sure to set a reference to System.Core and System.Xml.Linq
Dim states As XElement = XElement.Load("testXML.xml")

' Using LINQ
Dim foundNode As XElement
Dim query = From r As XElement In states...<Region> _
                  Where r.@<name> = "Milwaukee"
foundNode = query.FirstOrDefault()

' Using Lambda expression
foundNode = states...<Region>.Where(Function(r) r.@<name> =  _ 
                                 "Milwaukee").FirstOrDefault

This code first loads the XML file containing the XML. The next set of code can be done using LINQ or using Lambda expressions. Use either one, but not both. :-)

The C# code uses the XElement properties and methods. The VB code uses XML literals.

NOTE: The XElement properties and methods work in VB as well.

Enjoy!

NOTE: This post was created based on a prior post that included both finding a node and adding new nodes. This post separates the first step to provide a more straightforward example.

Filed under: , , , , , ,

Comments

# Finding a set of Nodes in an XML String

Thursday, March 25, 2010 11:48 AM by Deborah's Developer MindScape

In this prior post , I demonstrated how to find a node in an XML string. In this post, I expand on that

# re: Finding a Node in an XML String

Wednesday, May 19, 2010 12:35 AM by Bob

Very helpful. Thanks...

# re: Finding a Node in an XML String

Thursday, February 17, 2011 5:41 PM by JeffPGMT

I'm working on creating dynamic XML to use an XML template of an order form and insert current order data. I'm not sure if the word complex is accureate, here's a snipit

<TXLife>

 <UserAuthRequest>

         ...nodes/elements

 </UserAuthRequest>

         ...nodes/elements

 <TXLifeRequest>

   <OLife>

     <Holding>

         ...nodes/elements

       <Policy>

         ...nodes/elements

       </Policy>

       <Party id="Insured">

          <Person>

         ...nodes/elements

          </Person>

          <Address>

         ...nodes/elements

          </Address>

       </Party>

       <Party id="Producer">

          <Company>

         ...nodes/elements

          </Company>

       </Party>

     </Holding>

   </OLife>

 <TXLifeRequest>

</TXLife>

I want to update/replace just person found in Party tc="Insured"; I know that I can look down the tree because I know the order of the nodes/elements, but what if I just wanted to find an element that had an id="Insured" not knowing where in the tree that was?

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: