繁体   English   中英

如何遍历多个父元素xml文件的子元素c#

[英]how iterate through child element of multiple parents xml file c#

我有一个像这样的xml文件:

<post>
   <categories>
        <category ref="4527" />
        <category ref="4528" />
        <category ref="4529" />
        <category ref="4530" />
        <category ref="4531" />
   </categories>
</post>
<post>
   <categories>
        <category ref="4523" />
        <category ref="4524" />
        <category ref="4525" />
        <category ref="4526" />
        <category ref="4527" />
   </categories>
</post>

使用C#和.Net 4.5我想获得第一组类别引用号,然后处理它们,然后移动到下一组类别引用号并处理它们。 我希望有人可以指出我正确的方向。 我不知道如何使用XPath或Linq to XML或者这些甚至是正确的方法。 提前致谢。

在对一些非常聪明的人做出一些回应后,我能够使用Selman22的思路来帮助我编写一些XPath。 这是我提出的解决方案:

XmlDocument xdoc = new XmlDocument;
xdoc.Load(savePath);
XmlNode root = xdoc.DocumentElement;
// add the namespace 
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("bml", "http://www.blogml.com/2006/09/BlogML");
//puts the catagories elements into a list
XmlNodeList blogCatagories = root.SelectNodes("descendant::bml:post/bml:categories", nsmgr);
//loop throught list and place the attribute "ref" into a list and traverse each "ref"
foreach (XmlNode nodeCat in blogCatagories)
{
     XmlNodeList catagoryids = nodeCat.SelectNodes("descendant::bml:category/@ref", nsmgr);
    foreach (XmlNode nodeID in catagoryids)
    {
        Console.WriteLine(nodeID.InnerText.ToString());

    }
}

首先得到你的categories

var xdDoc = XDocument.Load(path);
var categories = xDoc.Descendants("categories").ToList();

然后遍历您的类别列表

foreach(var cat in categories)
{
  var numbers = cat.Elements("category").Select(c => (int)c.Attribute("ref"));

  foreach(var number in numbers)
  {
     // process your numbers

  }
}
var xdoc = XDocument.Load(path_to_xml);
var query = from p in xdoc.Root.Descendants("post")
            select p.Element("categories")
                    .Elements("category")
                    .Select(c => (int)c.Attribute("ref"))
                    .ToList();

此查询将返回迭代器,每次迭代时都会获得下一个类别引用序列序列。

foreach(List<int> references in query)
{
   // process list of references
   foreach(int reference in references)
      // process reference
}
XPathNavigator xml = new XPathDocument(filename).CreateNavigator();
foreach(XPathNavigator categories in xml.Select("//categories"))
{
    foreach(XPathNavigator category in categories.Select("category"))
    {
        string category_ref = category.GetAttribute("ref", string.Empty);
    }
    // do processing
}

在对一些非常聪明的人做出一些回应后,我能够使用Selman22的思路来帮助我编写一些XPath。

XmlDocument xdoc = new XmlDocument;
xdoc.Load(savePath);
XmlNode root = xdoc.DocumentElement;
// add the namespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("bml", "http://www.blogml.com/2006/09/BlogML");
//puts the catagories elements into a list
XmlNodeList blogCatagories = root.SelectNodes("descendant::bml:post/bml:categories", nsmgr);
//loop throught list and place the attribute "ref" into a list and traverse each "ref"
foreach (XmlNode nodeCat in blogCatagories)
{
    XmlNodeList catagoryids = nodeCat.SelectNodes("descendant::bml:category/@ref", nsmgr);
    foreach (XmlNode nodeID in catagoryids)
    {
        Console.WriteLine(nodeID.InnerText.ToString());

    }
}

我会使用XPathDocument和XPathNavigator,谷歌这样的很多例子

http://www.codegod.com/XPathDocument-XPathNavigator-XPathNodeIterator-sample-with-C-AID504.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM