Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read complicated xml into a DataSet

Posted on 2006-11-20
5
Medium Priority
?
326 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 500 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

719 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