2013-08-02 3 views
0

지금 xpCategories이foreach 루프가 제대로

<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을 보유하고

XPathNodeIterator xpCategories = GetCategories(); 

의 XPathNodeIterator 개체에 FOREACH하려고 내가 카테고리의 각 노드를 얻을 필요가 작동하지 "이드"가 루프 안에 들어갔습니다.이 같은 것을 몇 개 넣었습니까?

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

Ok, 위의 샘플을 시도해보십시오. – Damon

+0

예, 성공했습니다. 고마워요. – Athul

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