LINQ2XML Example

This is one of the code sample which I worked over a forum.

From the xml file given below, we need to select the nodes with category attribute of value pending.


  <fault category=”pending”>
    <text>Pending Fault found on</text>
  <fault category=”repaired”>
    <text>Repaired Fault on</text>


Following the LINQ2XML Code to attain the same:


XElement xElement = XElement.Load(@”c:\temp\xml.txt”);
            var list = (from e in xElement.Elements(“fault”) where e.Attribute(“category”).Value == “pending”
                 select e).ToList();

            foreach (XElement xe in list)


Leave a Reply

Your email address will not be published. Required fields are marked *