locked
How to Get XML Node from XDocument ? RRS feed

  • Question

  • 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 How can I Add New Nodes, Delete a Node and Update the selected node to that XDocument?

    How it is possible ????

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

    • Edited by Luttapi Thursday, April 16, 2009 4:28 AM edited
    Wednesday, April 15, 2009 3:38 PM

All replies

  • Hello,

    --Retriving from xmldocument
    DECLARE @myxml xml
    SET @Myxml ='<Contacts>
           <Node>
               <ID>123</ID>
               <Name>ABC</Name>
           </Node>
           <Node>
               <ID>124</ID>
               <Name>DEF</Name>
           </Node>
    </Contacts>'
    SELECT t.c.value('(ID)[1]','int') RegNo,
    t.c.value('(Name)[1]','nvarchar(10)') Node
    FROM @myxml.nodes('/Contacts/Node') t(c) 
    
    -- Inserting a node
    DECLARE @Myxml xml
    SET @Myxml = '<Contacts>
           <Node>
               <ID>123</ID>
               <Name>ABC</Name>
           </Node>
           <Node>
               <ID>124</ID>
               <Name>DEF</Name>
           </Node>
    </Contacts>'
    
    SET @Myxml.modify ('insert <Element1>My Element1</Element1> into(Contacts)[1]')
    SELECT @Myxml
    
    --- Replacing nothing but update
    
    DECLARE @Myxml xml
    SET @Myxml= '<Root>
    <Location LocationID="10" 
                LaborHours=".1"
                MachineHours=".2" >
    <step>Manufacturing step 1 at this work center</step>
    <step>Manufacturing step 2 at this work center</step>
    </Location>
    </Root>'
    --SELECT @myDoc
    
    SET @Myxml.modify('
      replace value of (/Root/Location[1]/@LaborHours)[1]
      with (
           if (count(/Root/Location[1]/step) > 3) then
             "45"
           else
              "34"
          )
    ')
    SELECT @Myxml
    
    --Deleting a node
    DECLARE @Myxml xml
    SET @Myxml = '<?Instructions for=TheWC.exe ?> 
    <Root>
     <!-- instructions for the 1st work center -->
    <Location LocationID="10" 
                LaborHours="1.1"
                MachineHours=".2" >Some text 1
    <step>Manufacturing step 1 at this work center</step>
    <step>Manufacturing step 2 at this work center</step>
    </Location>
    </Root>'
    SELECT @Myxml
    
    SET @Myxml.modify('
      delete /Root/Location/step[2]
    ')
    SELECT @Myxml
    Hope this is helpul.

    Thanks,
    Shanthi
    Friday, April 17, 2009 5:40 AM