tags:

views:

393

answers:

4

I have a heavier XML file with lots and lots of tree nodes. I need to pick-up some particular node (for example say Diet), under which there are multiple sections.

ie. Diet node occurs randomly in the XML, so i need to find the node as Diet and get its child elements and save it to DB.

Assume that Diet is not only one line, it has 10-12 entries underneath it (may be i can get its contents using InnerXML, but really can't get line by line nodes)

A: 

In XPath, it's just //Diet

To say more, I'd need to know more about your environment.

var doc = XDocument.Load("yourfile.xml");
var nodes = from d in doc.Desendants("Diet")
            select d;

foreach(var node in nodes)
{   // do stuff with node
}
James Curran
Thanks, i need to loop thro' the XML in asp.net/C# and take out nodes and save the node's value to DB. Please share some pseudo/codes.Thanks.
Karthick
@Karthick: what he posted is the pseudo-code you need, unless you also need help saving to the database. In that case, you need to tell us how you would like the data to appear in the database.
John Saunders
Thanks James for your inputs and thanks john for your comment. I am having hard time in completing this. The XML is very huge and i have XML and i don't have XSD with me to view XML in browser, so i am viewing in notepad and James pseudo code is great, it works for simple XMLs, but the XML i use is very much of nested and this doc.Desendants("Diet") returns null. Can you enlarge a bit with some more code? Thanks in advance.
Karthick
A: 

Depending on the structure of your XML file, you might try loading it into a DataSet (DataSet.ReadXML()) and see what DataTable it puts your Diet nodes into ... if it parses it ok then it is pretty simple to loop through the DataTable and get all your Diet node values.

I wrote a little toy app that opens XML like that, listing all the DataTables in a tree view then showing the table content in a grid. The VS project file for it is here or just an MSI to install it is here, if you want to see how a DataSet parses your XML file.

Ron Savage
-1: This almost never works, except for very simple XML formats. Also, there's very little reason to add the overhead of processing XML as relational data. Besides, even given a DataSet, how would you propose he find the `Diet` nodes and put their value into the database?
John Saunders
I disagree, it almost always does work - I posted the full source of an app that does it, and the code is simple and easy to understand. Did you actually try it, or just downvote for the heck of it?
Ron Savage
Thanks Ron for the inputs, when i select my XML file to test, it tells that relation is occurring in more than 2 instances. I don't know whether there is some issue in the XML file.
Karthick
Can you post an example of the XML? I've never run across an example and I'd like to troubleshoot it. :-)
Ron Savage
@Ron: I don't need to look at your code to know that the `DataSet` class can only represent XML that maps to a relational model - and nothing else. I have quite a bit of experience with XML schemas that cannot load into a `DataSet`. Simple example: table C as a child table of both A and B. Not relational, but simple XML.
John Saunders
Is this XML file an example of what you are describing? http://dot-dash-dot.com/files/DualChildExample.txt. I've found XML files that don't load into DataSets - but only found one type among the ~300 or so on my hard drive from various apps ...
Ron Savage
A: 

The pseudo code below, contains the XPath statement that would get you all elements who have a 'Diet' as parent. Since it produces a XmlNodeList you can walk every node and save it to the DB. For performance i would consider consolidating what you want to save, and then save it, not per line (round trip for every entry is sub-optimal)

XmlNodeList list = xDoc.DocumentElement.SelectNodes("//*[parent::Diet]"); foreach (XmlNode entry in list) { DAL.SaveToDatabase(entry); }

Hope this helps,

Marvin Smit
A: 
  1. Make sure you have added a reference to "System.xml.Linq'.
  2. Suck out all the Diet elements:

    XElement wholeFile = XElement.Load(@"C:\DietSampleXML.xml");
    IEnumerable<XElement> dietElements = wholeFile.Descendants("Diet");
    
  3. If you set a breakpoint and hover the mouse over "dietElements" and click "Results View", you will see all the Diet elements and their inner xml.

  4. Now iterate through dietElements to add each element and/or children to your database: "foreach (XElement x in dietElements) { ... }"

I tested this with the following xml:

<?xml version="1.0" encoding="utf-8" ?>
<TestElement>
  <Diet>
    <Name>Atkins</Name>
    <Colories>1000</Colories>
  </Diet>
  <TestElement2>
    <Diet>
      <Name>Donuts Only</Name>
      <Calories>1500</Calories>
    </Diet>
  </TestElement2>
  <TestElement3>
    <TestElement4>
      <Diet>
        <Name>Vegetarian</Name>
        <Calories>500</Calories>
      </Diet>
    </TestElement4>
  </TestElement3>
</TestElement>
Nick Miller