Solved

Read complicated xml into a DataSet

Posted on 2006-11-20
5
320 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

635 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