g_johnson
asked on
reading elements based on dynamic names, c#, linq to xml, part II
My xml file is set up like this:
<ord>
<item>
<sku>123456</sku>
</item>
</ord>
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";
try
{
foreach (string XMLFileName in FilesIn)
{
XDocument xDoc = XDocument.Load(XMLFileName );
foreach (var node in xDoc.XPathSelectElements(i temNo))
MessageBox.Show(node.Value );
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.
<ord>
<item>
<sku>123456</sku>
</item>
</ord>
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";
try
{
foreach (string XMLFileName in FilesIn)
{
XDocument xDoc = XDocument.Load(XMLFileName
foreach (var node in xDoc.XPathSelectElements(i
MessageBox.Show(node.Value
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.
Can you update your example XML to reflect this scenario?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
This helps tremendously and I think will solve my problem. If not, I will use the looping technique shown by masterpass