C# How to extract complete xml node set

后端 未结 4 540
名媛妹妹
名媛妹妹 2021-01-15 12:30

 
   
    Everyday Italian

        
相关标签:
4条回答
  • 2021-01-15 13:14

    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>
    
    0 讨论(0)
  • 2021-01-15 13:24

    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();
    
    0 讨论(0)
  • 2021-01-15 13:24

    Adding to Matthew's response:

    XmlDocument xDoc = new XmlDocument();
    // (Put code to populate xDoc here)
    XmlNodeList xNode = xDoc.SelectNodes(@"/bookstore/book[@category='COOKING']");
    

    xNode now equals Book of type COOKING.

    0 讨论(0)
  • 2021-01-15 13:27

    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);
                                }
                            }
                        }
    
    0 讨论(0)
提交回复
热议问题