2009-10-02 34 views
1
<?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來選擇完整的第一個節點集,例如從C#如何提取完整的XML節點設置

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

,這樣,XML的該塊可以存儲供以後使用。

鮑勃。

回答

3

假設這個XML存儲在一個叫做docXmlDocument中。

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> 
+0

謝謝丹。 鮑勃。 – 2009-10-02 21:02:30

1

該查詢將選擇該節點。你想獲得一組節點還是隻有一個節點?如果您只需要第n個節點子集,則可能必須自行將書店節點放回。

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

爲XmlDocument的...

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

,如果你只是想要的書節點,你可以做的,而不是根以上版本

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

我已經試過了。這隻會帶回「烹飪節點」。我想要的是將特定塊中的所有內容從帶回... 2009-10-02 19:42:55

+0

我正在讀取進入消息管道的xml消息。我需要存儲它們的特定部分,因此這些部分可以構建成新的複合消息,本質上是請求/答覆。所以我需要一種方法來從傳入消息中挑選選定的xml塊,以便以後使用它來構建回覆消息。 – 2009-10-02 19:44:49

+0

我正在使用SelectNode和SelectSingleNode獲取整個節點。 Xml.Linq類的工作原理是一樣的。 – 2009-10-02 19:47:53

0

加入Matthew's res ponse:

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

xNode現在等於COOKING類型的書。

+0

我想這會給你一個'XmlNodeList'而不是'XmlNode'。 – 2009-10-02 19:46:16

+0

是啊...你將需要得到第一個索引或使用SelectSingleNode – 2009-10-02 20:09:10

1

假設我要僅提取數據wherethe 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); 
          } 
         } 
        } 
+0

終於我有我的要求的代碼.. 'XmlDocument xDoc = new XmlDocument(); //(把代碼填充到xDoc這裏) XmlNodeList xNode = xDoc.SelectNodes(@「/ bookstore/book [@ category ='COOKING']/title」); – Gifted 2013-03-14 15:58:07