C# How to extract complete xml node set

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

 
   
    Everyday Italian         


        
4条回答
  •  独厮守ぢ
    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();
    

提交回复
热议问题