Nest data in Xml output in VB.NET

In this article you will learn how to nest data in Xml.
  • 2976

The Nested property of the DataRelation class lets you control how the Xml data for parent and child rows is generated. If you nest the Xml output, LineItem elements appear nested within their corresponding invoice elements. To generate nested Xml, you set the Nested property of the DataRelation object to true.

The following example show nested and unnested Xml for a dataset that has two table names Invoice and LineItem. The Invoice table has tow invoices. The invoice with Id 101 has one line item, and the invoice with Id 102 has two line items.


Nested XML

<Invoices>
    <Invoice Id="101" VendorId="80">
        <LineItem InvoiceId="101" Sequence="1">
           <ItemAmount>92.50</ItemAmount>
             <ItemDescription>Media</ItemDescription>
        </LineItem>
    </Invoices>
    <Invoice Id="102" VendorId="65">
        <LineItem InvoiceId="102" Sequence="1">
            <ItemAmount>425.21</ItemAmount>
             <ItemDescription>Printer</ItemDescription>
        </LineItem>
            <LineItem InvoiceId="102" Sequence="2">
              <ItemAmount>52.75</ItemAmount>
              <ItemDescription>Toner Cartridges</ItemDescription>
         </LineItem>
     </Invoices>
</
Invoices>


Unnested XML

<Invoices>
  <Invoice Id="101" VendorId="80">
      <Invoice Id="102" VendorId="65">
          <LineItem InvoiceId="101" Sequence="1">
             <ItemAmount>92.50</ItemAmount>
             <ItemDescription>Media</ItemDescription>
          </LineItem>
          <LineItem InvoiceId="102" Sequence="1">
              <ItemAmount>425.21</ItemAmount>
              <ItemDescription>Printer</ItemDescription>
          </LineItem>
          <LineItem InvoiceId="102" Sequence="2">
              <ItemAmount>52.75</ItemAmount>
              <ItemDescription>Toner Cartridges</ItemDescription>
          </LineItem>
   </Invoices>
</
Invoices>


Code that sets the Nested property

DsInvoices1.Relations("InvoiceLineItem").Nested = true

Description


You can use the Nested property of the DataRelation class to nest child data within its parent data when you use the GetXml or WriteXml. By default, the data isn't nested.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.