2009-06-03 17 views
3

Ich versuche, ein XML-Dokument durchlaufen, und ich bin immer noch das erste Element in der zweiten Iteration, nicht sicher, was ich vermisse. Kann jemand helfen? Ziemlich neu mit XpathDurchschleifen von Elementen mit xPath

string file = HttpContext.Current.Server.MapPath("~/XML/Locations.xml"); 

    Dictionary<string, Location> locationCollection = new Dictionary<string, Location>(); 

     XPathDocument xDocument = new XPathDocument(file); 
     XPathNavigator xPathNavigator = xDocument.CreateNavigator(); 

     foreach (XPathNavigator node in xPathNavigator.Select("//locations/*")) 
     { 
      string value = node.SelectSingleNode("/locations/location/cell").Value; 
     } 



    <?xml version="1.0" encoding="utf-8" ?> 
<locations> 
    <location> 
    <locationName>Glendale</locationName> 
    <street>3717 San Fernando Road</street> 
    <city>Glendale</city> 
    <state>CA</state> 
    <zipcode>91204</zipcode> 
    <generalManager>DJ Eldon</generalManager> 
    <phone>(818) 552‐6246</phone> 
    <tollFree>(888) 600‐6011</tollFree> 
    <fax>(818) 552‐6248</fax> 
    <cell>(347) 834‐2249</cell> 
    <counterEmail>[email protected]</counterEmail> 
    <directEmail>[email protected]</directEmail> 
    </location> 
    <location> 
    <locationName>Chicago</locationName> 
    <street>1301 S. Harlem Ave.</street> 
    <city>Chicago</city> 
    <state>IL</state> 
    <zipcode>60402</zipcode> 
    <generalManager>Dave Schnulle</generalManager> 
    <phone>(708) 749‐1500</phone> 
    <tollFree>(888) 966‐1500</tollFree> 
    <fax>(818) 552‐6248</fax> 
    <cell>(708) 749‐3800</cell> 
    <counterEmail>[email protected]</counterEmail> 
    <directEmail>[email protected]</directEmail> 
    </location> 
</locations> 

Antwort

11

Sie effektiv den Wert von node unter Verwendung eines führenden Schrägstrich ignoriert zurück zum Document-Root zu erhalten. Versuchen Sie stattdessen:

// This assumes that there are only location nodes under locations; 
// You may want to use //locations/location instead 
foreach (XPathNavigator node in xPathNavigator.Select("//locations/*")) 
{ 
    string value = node.SelectSingleNode("cell").Value; 
    // Use value 
} 

Having said that, gibt es einen Grund, Sie sich nicht in einer einzigen XPath-Abfrage zu tun?

// Name changed to avoid scrolling :) 
foreach (XPathNavigator node in navigator.Select("//locations/location/cell")) 
{ 
    string value = node.Value; 
    // Use value 
} 
+0

Danke, das hat super funktioniert ... Ja, es gibt einen Grund, warum ich keinen einzigen XPath mache, weil ich multipule Werte zu einer Sammlung hinzufügen werde. Danke nochmal! – BoredOfBinary

0

Versuchen Sie Folgendes:

XPathNodeIterator ni = xPathNavigator.Select("//locations/*"); 
while (ni.MoveNext()) 
{ 
    string value = ni.Current.Value); 
} 

Nur eine schnelle blurt, hoffen, dass es Ihnen hilft.

0

sollten Sie tun:

string value = node.SelectSingleNode("./cell").Value; 

Wenn Sie das tun xPathNavigator.Select ("// Standorte/*")), sind Sie bereits in/Orte/Lage, so dass Sie nur ein Element bewegen müssen nach unten der Knoten, Zelle in Ihrem Beispiel.

Verwandte Themen