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
Solved

Read complicated xml into a DataSet

Posted on 2006-11-20
5
302 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
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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