簡體   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