• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

reading elements based on dynamic names, c#, linq to xml, part II

My xml file is set up like this:


With the help of kaufMed in another question, I am able to set up a configuration file to tell my application:  You can find item number at <ord><item><sku> then use that information to read my xml file dynamically.,

The code looks like this:

            string itemNo = @"//ord/item/sku";

                foreach (string XMLFileName in FilesIn)
                    XDocument xDoc = XDocument.Load(XMLFileName);

                    foreach (var node in xDoc.XPathSelectElements(itemNo))

which successfully returns all the skus in the file.

In reality, a given file will have more than one order, a given order will have more than one item, and descending from the item element, there are other elements besides sku that I need to read.

Is there any way I can enhance the above code to look at a specific occurence of the given node?  Is there any way I can, for example, recognize that I have three orders in the file, then read for each order individually (they do have an order number attribute not shown above, like this:  <ord> OrderNumber=998889), and then look at the item elements within that specific order?

Thanks in advance for any thoughts you might have.
2 Solutions
käµfm³d 👽Commented:
Can you update your example XML to reflect this scenario?
Have a look here


using the [@nodeattribute='value'] along with the xpath can solve this issue!

Hope this helps!
Pierre FrançoisSenior consultantCommented:
When you compute

Open in new window

you get a list of nodes. You can store that as a variable and count how many nodes it has.

XmlNodeList nodelist = xDoc.XPathSelectElements(itemNo);
MessageBox.Show("Number of orders: " + nodelist.Count.ToString);
foreach (var node in nodelist) { ... ; }

Open in new window

Instead of parsing the whole list, you can restrict your XPath query to the specific order you want to retrieve. In that case, your XPathSelectElements call could look like

Open in new window

for selecting the second order. Caution: XPathSelectElement returns a node, XPathSelectElements returns a list of one or more nodes.

Selecting order number 998889 in the way described above happens with next XPath:
xDoc.XPathSelectElements("//ord[contains(text(), "OrderNumber=998889")]/item/sku")

Open in new window

but this is dangerous: OrderNumber=9988896666 will also be matched. If the XML file had tags like <order number="998889">, the XPath would be more robust and more beautiful:

Open in new window

but since you have no control on the XML structure, this is no more than wishful thinking.
g_johnsonAuthor Commented:
I will be checking out all of the suggestions as soon as I can turn my attention back to this project.  Thanks for your input and patience.
g_johnsonAuthor Commented:
This helps tremendously and I think will solve my problem.  If not, I will use the looping technique shown by masterpass
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now