Solved

Read complicated xml into a DataSet

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now