13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

When the tables are serialized, the Command slot is rendered as follows:ExtendedProperties holds a collection of objects and can accept values of any type, butyou might run <strong>in</strong>to trouble if you store values other than str<strong>in</strong>gs there. When the objectis serialized, any extended property is serialized as a str<strong>in</strong>g. In particular, the str<strong>in</strong>g iswhat the object's ToStr<strong>in</strong>g method returns. This can pose problems when the DataSetobject is deserialized.Not all types can be successfully and seamlessly rebuilt from a str<strong>in</strong>g. For example,consider the Color class. If you call ToStr<strong>in</strong>g on a Color object (say, Blue), you getsometh<strong>in</strong>g like Color [Blue]. However, no constructor on the Color class can rebuild avalid object from such a str<strong>in</strong>g. For this reason, pay careful attention to the nonstr<strong>in</strong>gtypes you store <strong>in</strong> the ExtendedProperties collection.Render<strong>in</strong>g Data RelationsA DataSet object can conta<strong>in</strong> one or more relations gathered under the Relationscollection property. A DataRelation object represents a parent/child relationship setbetween two DataTable objects. The connection takes place on the value of a match<strong>in</strong>gcolumn and is similar to a primary key/<strong>for</strong>eign key relationship. In ADO.<strong>NET</strong>, therelation is entirely implemented <strong>in</strong> memory and can have any card<strong>in</strong>ality: one-to-one,one-to-many, and even many-to-one.More often than not, a relation entails table constra<strong>in</strong>ts. In ADO.<strong>NET</strong>, you have twotypes of constra<strong>in</strong>ts: <strong>for</strong>eign-key constra<strong>in</strong>ts and unique constra<strong>in</strong>ts. A <strong>for</strong>eign-keyconstra<strong>in</strong>t denotes an action that occurs on the columns <strong>in</strong>volved <strong>in</strong> the relation when arow is either deleted or updated. A unique constra<strong>in</strong>t denotes a restriction on the parentcolumn whereby duplicate values are not allowed. How are relations rendered <strong>in</strong> <strong>XML</strong>?If no schema <strong>in</strong><strong>for</strong>mation is required, relations are simply ignored. When a schema isnot explicitly required, the <strong>XML</strong> representation of the DataSet object is a pla<strong>in</strong> snapshotof the currently stored data; any ancillary <strong>in</strong><strong>for</strong>mation is ignored. There are two ways toaccurately represent a DataRelation relation with<strong>in</strong> an <strong>XML</strong> schema: you can use the annotation or specify an element. The WriteXmlprocedure uses the latter solution.The msdata:Relationship AnnotationThe msdata:Relationship annotation is a <strong>Microsoft</strong> XSD extension that ADO.<strong>NET</strong> and<strong>XML</strong> programmers can use to explicitly specify a parent/child relationship between nonnestedtables <strong>in</strong> a schema. This annotation is ideal <strong>for</strong> express<strong>in</strong>g the content of aDataRelation object. In turn, the content of an msdata:Relationship annotation istrans<strong>for</strong>med <strong>in</strong>to a DataRelation object when ReadXml processes the <strong>XML</strong> file.Let's consider the follow<strong>in</strong>g relation:DataRelation rel = new DataRelation("Emp2Terr",ds.Tables["Employees"].Columns["employeeid"],ds.Tables["Territories"].Columns["employeeid"]);ds.Relations.Add(rel);The follow<strong>in</strong>g list<strong>in</strong>g shows how to serialize this relation to<strong>XML</strong>:335

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!