2013-08-02 107 views
0

我想的foreach創建XPathNodeIterator對象的foreach循環工作不正常

XPathNodeIterator xpCategories = GetCategories(); 

現在xpCategories持有這樣

<root> 
    <category numberofproducts="0"> 
    <id>format</id> 
    <name>Kopi/Print</name> 
    </category> 
    <category numberofproducts="1"> 
    <id>frankering</id> 
    <name>Kopi/Print</name> 
    </category> 
    <category numberofproducts="0"> 
    <id>gardbøjler</id> 
    <name>Møbler</name> 
    </category> 
    <category numberofproducts="0"> 
    <id>gardknager</id> 
    <name>Møbler</name> 
    </category> 
    <category numberofproducts="0"> 
    <id>gardspejle</id> 
    <name>Møbler</name> 
    </category> 

</root> 

一個XML的,我需要得到每個類別的節點「身份證」在loop.tried這樣的事情是這樣的

foreach (char str in xpCategories.Current.Value) 
    { 
     xpCategories.MoveNext(); 
    } 

但沒有用...可以任何一個指南我走向正確的道路?

回答

1

試試這個:

//Replace with your method to return the XML 
    XPathDocument document = new XPathDocument("DemoXML.xml"); 
    //***** 

    XPathNavigator navigator = document.CreateNavigator(); 

    XPathNodeIterator nodes = navigator.Select("/root/category/id"); 

    while (nodes.MoveNext()) 
     Console.WriteLine(nodes.Current.Value); 

編輯:

XPathNodeIterator xpCategories = GetCategories(); 
XPathNodeIterator xpCategoriesID = xpCategories.Current.Select("root/category/id"); 

while (xpCategoriesID.MoveNext()) 
    Console.WriteLine(xpCategoriesID.Current.Value); 
+0

我未能夠如我在從第三方dll..Ican't創建XPathNodeIterator形式獲取數據使用此代碼像你說的那樣創建新的XML文檔 – Athul

+0

好的,試試上面的示例? – Damon

+0

是的,工作..謝謝 – Athul

1
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id");  
while (xpCategories.MoveNext()) 
    Console.WriteLine(xpCategories.Current.Value);