Link to home
Start Free TrialLog in
Avatar of garthjh
garthjh

asked on

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!
Avatar of raterus
raterus
Flag of United States of America image

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
Avatar of garthjh
garthjh

ASKER

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?
SOLUTION
Avatar of BurntSky
BurntSky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Split between myself and raterus?
I agree with BurntSky, we've both offered a good advice as to how to accomplish this.  Split 50/50.
Avatar of garthjh

ASKER

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.