简体   繁体   English

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

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

I have an xml file like this: 我有一个像这样的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>

Using C# and .Net 4.5 I want to get the first set of category reference numbers, then process them, then move to the next set of category reference numbers and process them. 使用C#和.Net 4.5我想获得第一组类别引用号,然后处理它们,然后移动到下一组类别引用号并处理它们。 I am hoping that some one can point me in the right direction. 我希望有人可以指出我正确的方向。 I am not sure how to do this using XPath or with Linq to XML or if those are even the right approach. 我不知道如何使用XPath或Linq to XML或者这些甚至是正确的方法。 Thanks in advance. 提前致谢。

After some responses to some very smart people I was able to use Selman22's train of thought to help me write some XPath. 在对一些非常聪明的人做出一些回应后,我能够使用Selman22的思路来帮助我编写一些XPath。 Here is the solution I came up with: 这是我提出的解决方案:

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());

    }
}

First get your categories 首先得到你的categories

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

Then loop through your category list 然后遍历您的类别列表

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();

This query will return iterator which will get next sequence of category reference numbers each time you are iterating it. 此查询将返回迭代器,每次迭代时都会获得下一个类别引用序列序列。

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
}

After some responses to some very smart people I was able to use Selman22's train of thought to help me write some XPath. 在对一些非常聪明的人做出一些回应后,我能够使用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());

    }
}

I would use XPathDocument and XPathNavigator, lots of examples on google like this 我会使用XPathDocument和XPathNavigator,谷歌这样的很多例子

http://www.codegod.com/XPathDocument-XPathNavigator-XPathNodeIterator-sample-with-C-AID504.aspx 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