Solved

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

Posted on 2012-03-29
5
369 Views
Last Modified: 2012-04-04
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(itemNo))
                        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.
0
Comment
Question by:g_johnson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37785410
Can you update your example XML to reflect this scenario?
0
 
LVL 21

Assisted Solution

by:masterpass
masterpass earned 100 total points
ID: 37785603
Have a look here

http://www.java2s.com/Code/CSharp/XML/ReadXMLnodeusingtheXMLpath.htm

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

Hope this helps!
0
 
LVL 10

Accepted Solution

by:
pfrancois earned 400 total points
ID: 37786078
When you compute
xDoc.XPathSelectElements(itemNo)

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
xDoc.XPathSelectElement("//ord/item/sku[2]")

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:
xDoc.XPathSelectElements("//ord[@number="998889"]/item/sku")

Open in new window

but since you have no control on the XML structure, this is no more than wishful thinking.
0
 
LVL 4

Author Comment

by:g_johnson
ID: 37797358
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.
0
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 37808752
This helps tremendously and I think will solve my problem.  If not, I will use the looping technique shown by masterpass
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to remove duplicate code from my project 5 66
How we can add Dten600.dll in GAC in c#. 18 55
Send parameters via datatable ajax call 4 48
async questions 5 50
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question