Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

reading xml elements based on dynamic element names using VS2008 c# and linq to xml

Let's say that I have an xml file that looks like this:
<ord>
  <line>
    <sku>123456</sku>
  </line>
</ord>

Let's also say that I have no control over the xml file (it's incoming) and the producer of the file could change it at any time to be:
<order>
  <lineinfo>
    <itemnumber>123456</itemnumber>
  </lineinfo>
</order>

In my VS2008 c# program, is it even possible to set up a configuration that says something like "find the item number at <ord><line><sku>" then read the xml file (using linq to xml) "dynamically"?

I guess, another way of putting it would be can I assign string variables, such as El1, El2, El3 and set them equal to "ord", "line", and "sku", respectively, then read for those elements using the variable names instead of the element names?

Thanks in advance for your thoughts on this.
Avatar of kaufmed
kaufmed
Flag of United States of America image

What are you trying to extract? I'm having an interesting time trying to determine what the end goal is. It seems like you want the flexibility of retrieving data from the XML without having to worry (excessively) about the incoming XML structure. But I think I might be missing something with regard to El1, 2, & 3.
Avatar of g_johnson

ASKER

I think a better way of putting it is I don't want to hard-code the element names.  If the xml file provider should change an element name, I want to be able to change that in a configuration file instead of re-coding the application.

I liken this to parsing a tab-delimited text file.  In order to avoid hard coding, I will put an entry in my config file to say that item number is in the third field.  Then, if the incoming file format changes, and all of a sudden the item number is in the thirteenth field, all I have to do is change my config file.  I don't have to re-code.

Does that make sense?
Sure. So what exactly would you be storing in El1 and El2? It seems to me that El3 corresponds to the value of "sku" (or "itemnumber", depending). If that's the case, what would you be storing for "ord" (and "order") and "line" (and "lineinfo")?
I don't know that I need El1 and El2, but what I want to do is to say

"item number" is actually found in the element "sku" (El3) which is a subelement of "line" (El2) which is a subelement of "ord" (El1).

Then I would want to design my linq to xml statement to use the variables instead of the actual element names.

Again, I don't know if this is possible.  I'm looking for ideas and this is the best I could come up with, even though I'm not sure how it would work.

Meanwhile I'm asking my client if he will accept a hard-coded solution which puts the burden on the provider of the xml file.  :-)  Really, though, I would like to make it dynamic and configurable and avoid hard-coding if at all possible.
I think XPath would probably suit this scenario. For example, using the XML you posted above (file1 and file2, respectively) and some XPath magic:

using System;
using System.Xml.Linq;
using System.Xml.XPath;

namespace _27654891
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument xdoc = XDocument.Load("file1.xml");

            foreach (var node in xdoc.XPathSelectElements("//ord/line/sku"))
            {
                Console.WriteLine(node.Value);
            }

            xdoc = XDocument.Load("file2.xml");

            foreach (var node in xdoc.XPathSelectElements("//order/lineinfo/itemnumber"))
            {
                Console.WriteLine(node.Value);
            }

            Console.ReadKey();
        }
    }
}

Open in new window


The XPath breaks down as:

At any level ( // ) look for an "ord" element ( ord ), and it should have a child named "line" ( /line ), and that child should have a child named "sku" ( /sku ). The effect of the XPath selection is that all "sku" element are chosen, provided they have the appropriate parents. The XPath for the file2 is the same in meaning; just substitute the appropriate name in each part of the XPath.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent!  I'd give you more than 500 points if I could!

In a couple of minutes, I'm going to post another question to take this concept a little further.