C# How to extract complete xml node set

12,174

Solution 1

Let's say this XML is stored in an XmlDocument called doc.

XmlElement docRoot = doc.DocumentElement;
XmlNode cookingNode = docRoot.SelectSingleNode("./book[@category='COOKING']");

I tested this and added this line to verify:

Console.WriteLine(cookingNode.OuterXml);

Here was the output:

<book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada
De Laurentiis</author><year>2005</year><price>30.00</price></book>

Solution 2

This query will select that node. Are you trying to get a set of nodes or just the single one? You might have to put the bookstore node back yourself if you only want th subset of nodes.

/bookstore/book[@category='COOKING']

as XmlDocument ...

var x = new XmlDocument();
x.Load("XmlFile1.xml");
var ns = x.SelectSingleNode("/bookstore/book[@category='COOKING']");

var res = ns.OuterXml;

as XDocument ...

var x = XDocument.Load("XmlFile1.xml");

var root = new XElement("bookstore",
    from book in x.Element("bookstore").Elements("book")
    where book.Attribute("category").Value == "COOKING"
    select book
    );

if you just want the book node you can do this instead of the root version above

var book = x.Element("bookstore")
    .Elements("book")
    .Where(n => n.Attribute("category").Value == "COOKING")
    .First();

Solution 3

suppose I want to extract only the data wherethe xml file is as follows ..

<book category="COOKING"> 
    <title lang="en">Everyday Italian</title>
    <author auth="up">Giada De Laurentiis</author>    
    <year>2005</year>
   <price>30.00</price>
  </book>

the final result on the list view should look like this

  lang       auth
  en          up

I have coded as follows ..

XmlNodeList elemList = doc.GetElementsByTagName("book");
                    for (int j = 0; j < elemList.Count; j++)
                    {
                        if (elemList[j].Attributes["category"].Value == "COOKING")
                        {
                            XmlNodeList elemList1 = doc.GetElementsByTagName("author");
                            for (int i = 0; i < elemList1.Count; i++)
                            {
                                string attrVal = elemList1[i].Attributes["lang"].Value;
                                string attrVal1 = elemList1[i].Attributes["auth"].Value;

                                ListViewItem lvi = new ListViewItem();

                                    lvi.SubItems.Add(attrVal1);
                                    lvi.SubItems.Add(attrVal1);
                                }
                                listView1.Items.Add(lvi);
                            }
                        }
                    }
Share:
12,174
scope_creep
Author by

scope_creep

Updated on June 04, 2022

Comments

  • scope_creep
    scope_creep about 2 years
    <?xml version="1.0" encoding="ISO-8859-1"?>
     <bookstore>
      <book category="COOKING"> 
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>    
        <year>2005</year>
       <price>30.00</price>
      </book>
    
      <book category="CHILDREN">
       <title lang="en">Harry Potter</title>
       <author>J K. Rowling</author>
       <year>2005</year>
       <price>29.99</price>
      </book>
    
      <book category="WEB">
       <title lang="en">XQuery Kick Start</title>
       <author>James McGovern</author>
       <author>Per Bothner</author>
       <author>Kurt Cagle</author>
       <author>James Linn</author>
       <author>Vaidyanathan Nagarajan</author>
       <year>2003</year>
       <price>49.99</price>
      </book>
    
      <book category="WEB">
       <title lang="en">Learning XML</title>
       <author>Erik T. Ray</author>
       <year>2003</year>
       <price>39.95</price>
      </book>
    
    </bookstore>
    

    Is their any way using XPath to select the complete first node set, for example from

     <book category="COOKING">  
      to 
     </book>, 
    

    so that, that chunk of xml can be stored for later use.

    Bob.