views:

4700

answers:

3

Hi,

How to Get an XML Element from XDocument using LINQ ?

Suppose I have an XDocument Named XMLDoc which is shown below:

<Contacts>
       <Node>
           <ID>123</ID>
           <Name>ABC</Name>
       </Node>
       <Node>
           <ID>124</ID>
           <Name>DEF</Name>
       </Node>
</Contacts>

XElement Contacts = from xml2 in XMLDoc.Elements("Contacts").Elements("Node")
                    where xml2.Element("ID").Value == variable
                    select xml2;

But I am getting Error "Object Reference is NOT to set....."

How to get a particular Node from a XML file using LINQ ? And I want to update some values in that node ?

How it is possible ????

Thanks in advance.........

+1  A: 

The .Elements operation returns a LIST of XElements - but what you really want is a SINGLE element. Add this:

XElement Contacts = (from xml2 in XMLDoc.Elements("Contacts").Elements("Node")
                    where xml2.Element("ID").Value == variable
                    select xml2).FirstOrDefault();

This way, you tell LINQ to give you the first (or NULL, if none are there) from that LIST of XElements you're selecting.

Marc

marc_s
Your edit made it clear. He is falling prey to the classic mistake of trying to use the query as the result set. http://stackoverflow.com/questions/594919/a-question-about-linq-to-sql/594975#594975 My answer to a similar question
toast
Hi, Thanks for the response.But I am still getting the error.......Through this query, how can I add new nodes, update and delete node in an XDocument ????
If you want to add new elements, you need to grab the XElement you want to add something to. What do you want to do? You can't do all through one single query...
marc_s
A: 

Hi, Thanks for the response. But I am still getting the error....... Through this query, how can I add new nodes, update and delete a node in the XDocument ????

+1  A: 

Response to additional question posted by OP.

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<Contacts>
  <Node>
    <ID>123</ID>
    <Name>ABC</Name>
  </Node>
  <Node>
    <ID>124</ID>
    <Name>DEF</Name>
  </Node>
</Contacts>

Select a single node:

XDocument XMLDoc = XDocument.Load("test.xml");
string id = "123"; // id to be selected

XElement Contact = (from xml2 in XMLDoc.Descendants("Node")
                    where xml2.Element("ID").Value == id
                    select xml2).FirstOrDefault();

Console.WriteLine(Contact.ToString());

Delete a single node:

XDocument XMLDoc = XDocument.Load("test.xml");
string id = "123";

var Contact = (from xml2 in XMLDoc.Descendants("Node")
               where xml2.Element("ID").Value == id
               select xml2).FirstOrDefault();

Contact.Remove();
XMLDoc.Save("test.xml");

Add new node:

XDocument XMLDoc = XDocument.Load("test.xml");

XElement newNode = new XElement("Node",
    new XElement("ID", "500"),
    new XElement("Name", "Whatever")
);

XMLDoc.Element("Contacts").Add(newNode);
XMLDoc.Save("test.xml");

There's also a very nice tutorial to parsing XMLs here. Hope it helps.

Ondrej Slinták