Solved

Read complicated xml into a DataSet

Posted on 2006-11-20
5
313 Views
Last Modified: 2010-04-16
I am trying to read in an XML doc ument into a DataSet such that I can get the data and potentially re-set values as well. The XML document looks as follows:

<MyDoc>
   <Password>
      <value>test</value>
      <remark>Password for database.</remark>
   </Password>
   <FilePath>
      <value>\NativePath</value>
      <remark>Path for location of file.</remark>
   </FilePath>
   <SEC-A01>
      <Paths>
            <pval>progs.foo</pval>
            <pvalRemark>Relative path to foo 1.</pvalRemark>
            <pval>progs.bar</pval>
            <pvalRemark>Relative path to bar 1.</pvalRemark>
                                 .
                                 .
                                 .
      </Paths>
      <ConfigTime>progs.AdmitDateTime</ConfigTime>
      <ConfigTimeRemark>Relative path to time field.</ConfigTimeRemark>
   </SEC-A01>
</MyDoc>

As you can see there is a bit of nesting here so getting tables and rows is a bit different. The "Paths" section can have any number of 'pval' / 'pvalRemark' entries.

Simply using ds.Tables["SEC-A01"].Rows[0]["pval"].toString() or any combination that I've tried throws exceptions.

How can I use DataSet/DataTable/DataRow (to which ever combination) to get the data from this xml doc?

Thanks,
ipaman
0
Comment
Question by:ipaman
[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
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17979497
Reading your XML straight into a DataSet produces six tables. "pval" and "pvalRemark" are put into their own tables, so you can access all "pval" records using:

    DataSet ds = new DataSet();
    ds.ReadXml(@"Your_xml_file.xml");

    foreach (DataRow row in ds.Tables["pval"].Rows)
    {
        MessageBox.Show(row[0].ToString());
    }
0
 

Author Comment

by:ipaman
ID: 17979955
carl tawn.....OK, I forgot to mention one other attribute of this xml document. (brain still asleep)

There will be other sections like <SEC-A01>. Except they will be <SEC-A02>, <SEC-A03>, etc... and
I will need to differentiate between the pval in SEC-A01 and the pval in SEC-A02, etc...

What would be the code to differentiate these sections?

thanks in advance,
ipaman

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17980149
That may be a little tricky due to the structure of your document. Could you not just parse the document as an XML file rather than reading it into a DataSet ?
0
 

Author Comment

by:ipaman
ID: 17980216
what is the other way of parsing the xml doc? (Not xerces!)

I thought bringing it in to a DataSet would be the quickest and easiest.....
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 17980589
You can use the objects in the System.Xml namespace. The DataSet is only really a viable option if you have your XML in a format that can easily be loaded into related tables.

For example, the following code block will grab the SEC-A01 node and loop through all of its "pval" and "pvalRemark" nodes:

            XmlDocument doc = new XmlDocument();
            doc.Load("your_xml_document.xml");

            XmlNode node = doc.SelectSingleNode("/MyDoc/SEC-A01");
            foreach (XmlNode child in node.ChildNodes[0].ChildNodes)
            {
                MessageBox.Show(child.Name + ": " + child.InnerText);
            }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# DataTable (in memory) vs SQL Lookups 4 74
C# LINQ 5 43
How we can add Dten600.dll in GAC in c#. 18 55
Windows Universal App 22 61
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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