C# 使用 xPath 循环遍历项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/945512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:57:26  来源:igfitidea点击:

looping through items using xPath

c#asp.netxpath

提问by BoredOfBinary

I am tryng to loop through an xml doc and I am still getting the first element in the second iteration, not sure what I am missing. Can anyone help? Pretty new with Xpath

我正在尝试遍历 xml 文档,但我仍然在第二次迭代中获得第一个元素,不确定我错过了什么。任何人都可以帮忙吗?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>

采纳答案by Jon Skeet

You're effectively ignoring the value of nodeby using a leading slash to get back to the document root. Try this instead:

node通过使用前导斜杠返回文档根目录,您实际上忽略了 的值。试试这个:

// 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, is there any reason you're not doing it in a single XPath query?

话虽如此,您是否有任何理由不在单个 XPath 查询中执行此操作?

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

回答by Onots

Try the following:

请尝试以下操作:

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

Just a quick blurt, hope it helps you.

简单粗暴的说一下,希望对你有帮助。

回答by jeremy

you should do:

你应该做:

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

When you do xPathNavigator.Select("//locations/*")), you're already inside /locations/location so you need to move just one element down the node, cell in your example.

当您执行 xPathNavigator.Select("//locations/*")) 时,您已经在 /locations/location 中,因此您只需将一个元素向下移动到节点,在您的示例中为单元格。