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

时间:2014-01-09 16:52:27

标签: c# .net xml xpath linq-to-xml

我有一个像这样的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());

    }
}

5 个答案:

答案 0 :(得分:3)

首先获取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

  }
}

答案 1 :(得分:0)

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
}

答案 2 :(得分:0)

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
}

答案 3 :(得分:0)

在对一些非常聪明的人做出一些回应之后,我能够使用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());

    }
}

答案 4 :(得分:-1)

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

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