代码之家  ›  专栏  ›  技术社区  ›  scope_creep

C如何提取完整的XML节点集

  •  1
  • scope_creep  · 技术社区  · 15 年前
    <?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>
    

    他们是否可以使用xpath来选择完整的第一个节点集,例如从

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

    这样,XML块就可以存储起来供以后使用。

    鲍勃。

    4 回复  |  直到 11 年前
        1
  •  3
  •   Dan Tao    15 年前

    假设此XML存储在 XmlDocument 打电话 doc .

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

    我对此进行了测试,并添加了此行以验证:

    Console.WriteLine(cookingNode.OuterXml);
    

    结果如下:

    <book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada
    De Laurentiis</author><year>2005</year><price>30.00</price></book>
    
        2
  •  1
  •   Matthew Whited    15 年前

    此查询将选择该节点。您是要获取一组节点还是只获取一个节点?如果只需要节点的子集,那么您可能需要将Bookbook节点放回自己的位置。

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

    作为XML文档…

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

    作为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
        );
    

    如果只需要book节点,可以这样做,而不是使用上面的根版本。

    var book = x.Element("bookstore")
        .Elements("book")
        .Where(n => n.Attribute("category").Value == "COOKING")
        .First();
    
        3
  •  1
  •   Gifted    11 年前

    假设我只想提取XML文件如下所示的数据。

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

    列表视图上的最终结果应该如下所示

      lang       auth
      en          up
    

    我已经编码如下……

    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);
                                }
                            }
                        }
    
        4
  •  0
  •   tsilb    15 年前

    补充马修的回答:

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

    xnode现在等于烹饪类书籍。