简体   繁体   中英

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

I have an xml file like this:

<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. 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. 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. 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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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