XML that comes from a database, whether
generated directly from a DataSet
or through an
XmlDataDocument
, is inherently
relational. That is, each table is represented
by a single element, and its columns are represented by elements
within it. Relations between tables are indicated by foreign key
constraints and row identifiers. This makes perfect sense for
relational data, but sometimes you might want to use a more
hierarchical format.
XML is ideal for representing hierarchical data, because it is itself a tree-oriented format. The data from the coupon database could easily be represented in a combination of relational hierarchical XML structures, as shown in Example 11-12.
<AngusHardware> <customers> <customer_id>1</customer_id> <name>Mark's Roofing</name> <address1>99 Beltline Pkwy</address1> <address2>Suite 100</address2> <city>Wannaque</city> <state>NH</state> <zip>05461 </zip> </customers> <coupons> <coupon_code>077GH </coupon_code> <discount_amount>15</discount_amount> <discount_type>0</discount_type> <expiration_date>2002-11-09T14:17:41.6370000-05:00</expiration_date> </coupons> <coupons> <coupon_code>665RQ </coupon_code> <discount_amount>15</discount_amount> <discount_type>0</discount_type> <expiration_date>2002-11-30T00:00:00.0000000-05:00</expiration_date> <coupon_redemptions> <coupon_code>665RQ </coupon_code> <total_discount>21.5</total_discount> <redemption_date>2002-11-10T00:00:00.0000000-05:00</redemption_date> <customer_id>1</customer_id> </coupon_redemptions> </coupons> <coupons> <coupon_code>81BIN </coupon_code> <discount_amount>10</discount_amount> <discount_type>1</discount_type> <expiration_date>2003-01-31T00:00:00.0000000-05:00</expiration_date> </coupons> <coupons> <coupon_code>99GGY </coupon_code> <discount_amount>5</discount_amount> <discount_type>0</discount_type> <expiration_date>2002-12-31T00:00:00.0000000-05:00</expiration_date> </coupons> </AngusHardware>
This differs from the straight relational output from the
DataSet
, shown in Example 11-13.
<AngusHardware> <customers> <customer_id>1</customer_id> <name>Mark's Roofing</name> <address1>99 Beltline Pkwy</address1> <address2>Suite 100</address2> <city>Wannaque</city> <state>NH</state> <zip>05461 </zip> </customers> <coupons> <coupon_code>077GH </coupon_code> <discount_amount>15</discount_amount> <discount_type>0</discount_type> <expiration_date>2002-11-09T14:17:41.6370000-05:00</expiration_date> </coupons> <coupons> <coupon_code>665RQ </coupon_code> <discount_amount>15</discount_amount> <discount_type>0</discount_type> <expiration_date>2002-11-30T00:00:00.0000000-05:00</expiration_date> </coupons> <coupons> <coupon_code>81BIN </coupon_code> <discount_amount>10</discount_amount> <discount_type>1</discount_type> <expiration_date>2003-01-31T00:00:00.0000000-05:00</expiration_date> </coupons> <coupons> <coupon_code>99GGY </coupon_code> <discount_amount>5</discount_amount> <discount_type>0</discount_type> <expiration_date>2002-12-31T00:00:00.0000000-05:00</expiration_date> </coupons> <coupon_redemptions> <coupon_code>665RQ </coupon_code> <total_discount>21.5</total_discount> <redemption_date>2002-11-10T00:00:00.0000000-05:00</redemption_date> <customer_id>1</customer_id> </coupon_redemptions> </AngusHardware>
There is one major difference between the relational and hierarchical
views. In the relational view, each row’s elements
are direct children of the root element. In the hierarchical view,
however, the coupon_redemptions
element is a child
of the coupons
element; because any
coupon_redemptions
row can only be related to
exactly one coupons
row, it makes sense to present
them in this hierarchical fashion.
How can you have the DataSet
present this
hierarchical XML view of the data? There are a couple of ways:
transformation and synchronizing data.
52.14.240.178