• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

Parse XML data into Nested Datagrid/Repeater

Hello Experts,

The answer I need is a bit of code to parse an XML document into a nested datagrid for display.  I don't really care about the mechanics, other than at the end I should have a datagrid with nested detail data and it should be pretty efficient.  So, the Datagrid will have a listing of accounts, the details of which will come from different levels of XML, then a nested grid/repeater/whatever that lists more details about the account.

The XML:

<accountRecord>
  <accountID>data</accountID>
  <accountName>data</accountName>
  <accountType>data</accountType>
  <balance>
    <balAmount>0.00</balAmount
  </balance>
  <accountDetail>
     <source id="number">
          <sourceDescription>data</sourceDescription>
          <sourceValue>0.00</sourceValue
     </sourceID>
  </accountDetail>
</accountRecord>

Where there will be 1 to infinity number of <accountRecord>'s and 1 to infinity number of <source>'s.

So, an example set of records could be something like:

<accountRecord>
  <accountID>12345</accountID>
  <accountName>My Account</accountName>
  <accountType>Individual</accountType>
  <balance>
    <balAmount>100.00</balAmount
  </balance>
  <accountDetail>
     <source id="001">
          <sourceDescription>Sub Account 1</sourceDescription>
          <sourceValue>25.00</sourceValue
     </sourceID>
     <source id="002">
          <sourceDescription>Sub Account 2</sourceDescription>
          <sourceValue>75.00</sourceValue
     </sourceID>
  </accountDetail>
</accountRecord>
<accountRecord>
  <accountID>54321</accountID>
  <accountName>Joint Account</accountName>
  <accountType>Joint</accountType>
  <balance>
    <balAmount>200.00</balAmount>
  </balance>
  <accountDetail>
     <source id="001">
          <sourceDescription>Sub Account 1</sourceDescription>
          <sourceValue>200.00</sourceValue
     </sourceID>
  </accountDetail>
</accountRecord>

And what I want to display is:

AccountID          AccountName               balAmount
-----------------   --------------------------  ------------------
12345                My Account                  100.00
                         
                         Source Id-Description             sourceValue
                         ----------------------------------------------
                         001-Sub Account 1                 25.00
                         002-Sub Account 2                 75.00

AccountID          AccountName               balAmount
-----------------   --------------------------  ------------------
54321                Joint Account                200.00
                         
                         Source Id-Description             sourceValue
                         ----------------------------------------------
                         001-Sub Account 1                 200.00

So that's it.  I know the XML could probably be done better, but I do not have control over it, it is from a 3rd party.  The solution I am looking for is in ASP.Net, VB, taking the input XML and outputting what is shown above.  Again, any transformations, etc. in the middle I really don't care about, but the outside level needs to be a datagrid (for sorting).

Please help, a quick, efficient solution will earn bonus points.

Thanks for your help!
0
garthjh
Asked:
garthjh
  • 3
  • 2
  • 2
2 Solutions
 
raterusCommented:
garthjh,

Have you ever looked into XML Deserialization to your own custom object?  Essentially you'd create your own object, that describes an "accountRecord", since your xml is nested, you'll need to create objects for the balance, and accountDetail as well.  Anyway, you run it through the deserializer which will take your xml and instantiate all the objects you need for the operation.  Once it's like this, you could turn around and bind it to a DataGrid.  This is certainly just the basics, but it would work.

--Michael
0
 
garthjhAuthor Commented:
Hi Michael,

I started looking at this based on your suggestion, but the resources available seem to be incomplete and/or lacking.  I can create a custom object with all the properties, etc. but am confused about how to load the data into it/instance of it.  Can you point me to a reference?
0
 
BurntSkyCommented:
Theres a tool that comes with Visual Studio .NET called "xsd.exe".  The tool can be used to infer XSD schemas from an XML file.  It can also generate classes from these XSDs.  You can then use the classes it generates to deserialize your XML files into objects.  (Make sure your XML file is well-formed; the one you posted in your original post is not).

Use these two command lines to create the class:
xsd.exe AccountRecord.xml
xsd.exe AccountRecord.xsd /c

Now you should have an AccountRecord.cs class that represents the XML file.  Include this into your project.

To serialize and deserialize your objects, you can use this:

// Deserialization
AccountRecord ar;
XmlSerializer s = new XmlSerializer( typeof( AccountRecord) );
TextReader r = new StreamReader( "c:\ar.xml" );
newList = (AccountRecord)s.Deserialize( r );
r.Close();

// Serialization
AccountRecord ar = new AccountRecord();
XmlSerializer s = new XmlSerializer( typeof( AccountRecord) );
TextWriter w = new StreamWriter( @"c:\ar.xml" );
s.Serialize( w, ar);
w.Close();


Now you have an actual object you can work with.  This should make custom binding scenarios much easier.
0
Technology Partners: 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!

 
raterusCommented:
BurntSky said pretty much what I wanted to say, but if you didn't want to use xsd.exe (your objects seem simple enough), look up the XmlAttribute and XmlRoot attributes

You place XmlRoot in front of class declarations, and XmlAttribute infront of properties for the class.  They basically clue in the serializer where to put what during the (de)serialization process.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationxmlattributesclassxmlroottopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationxmlattributesclassxmlroottopic.asp

http://www.4guysfromrolla.com/webtech/012302-1.shtml
0
 
BurntSkyCommented:
Split between myself and raterus?
0
 
raterusCommented:
I agree with BurntSky, we've both offered a good advice as to how to accomplish this.  Split 50/50.
0
 
garthjhAuthor Commented:
I appreciated the serialization/deserialization links provided as they helped to understand the process of dealing with XML such as this.  While I did not utilize the suggestions for this particular solution, it would definitely be useful for others to try.

Basically, the solution I employed involved using the built in XML readers in .Net to parse the information into two datatables, each containing a column I could match against.  One datatable was used to populate the 'outside' grid and then I created a function to iterate through the detail datatable when the grid is populated to add a table within the grid.

Probably not the most elegant solution, but it solved the problem in a timely manner.  I think that had the XML been in a more friendly format (i.e. the vendor provided a schema) the solution would have been simpler.

Thanks to all for the assistance and good resources.
0

Featured Post

Technology Partners: 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!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now