XML Externalization in .NET


As mentioned before, all data communication within .NET and outside of .NET can be done via XML. By definition, any DataSet can be transported anywhere via its XML externalization. In fact, the DataSet is stored at all times in its XML form.

Figure 46.5 shows the Customers table in the Northwind database. Listing 46.2 is the associated XML Schema Definition for Customers (compliant with the XML Schema definition language standards). This is easily generated in Visual Studio .NET or via the ADO.NET XML Schema generation command (the WriteXMLSchema method of the DataSet ).

Listing 46.2 The XML Schema File for the Customers DataSet
 <?xml version="1.0" encoding="utf-8"?> <xs:schema id="CustDataSet" xmlns=""     xmlns:xs="http://www.w3.org/2001/XMLSchema"     xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">   <xs:element name="CustDataSet" msdata:IsDataSet="true">     <xs:complexType>       <xs:choice maxOccurs="unbounded">         <xs:element name="Customers">           <xs:complexType>             <xs:sequence>               <xs:element name="CustomerID" type="xs:string" minOccurs="0" />               <xs:element name="CompanyName" type="xs:string" minOccurs="0" />               <xs:element name="ContactName" type="xs:string" minOccurs="0" />               <xs:element name="ContactTitle" type="xs:string" minOccurs="0" />               <xs:element name="Address" type="xs:string" minOccurs="0" />               <xs:element name="City" type="xs:string" minOccurs="0" />               <xs:element name="Region" type="xs:string" minOccurs="0" />               <xs:element name="PostalCode" type="xs:string" minOccurs="0" />               <xs:element name="Country" type="xs:string" minOccurs="0" />               <xs:element name="Phone" type="xs:string" minOccurs="0" />               <xs:element name="Fax" type="xs:string" minOccurs="0" />             </xs:sequence>           </xs:complexType>         </xs:element>       </xs:choice>     </xs:complexType>   </xs:element> </xs:schema> 
Figure 46.5. The Customers table.

graphics/46fig05.jpg

If you need a much more complex DataSet that, for instance, includes customers (all elements) and their associated orders (all elements), the XML coding is a bit longer. Figure 46.6 shows the relationship that must be traversed from the Customers table to the Orders table. This translates into a more complex XML Schema definition that contains both customers and orders. Remember, it must allow you to traverse a relationship (from parent customers to their child orders) using one DataSet (as shown in Listing 46.3).

Listing 46.3 The XML Schema File for the Customers and Orders DataSet
 <?xml version="1.0" encoding="utf-8"?> <xs:schema id="CustDataSetO" xmlns=""     xmlns:xs="http://www.w3.org/2001/XMLSchema"     xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">   <xs:element name="CustDataSetO" msdata:IsDataSet="true">     <xs:complexType>       <xs:choice maxOccurs="unbounded">         <xs:element name="Customers">           <xs:complexType>             <xs:sequence>               <xs:element name="CustomerID" type="xs:string" minOccurs="0" />               <xs:element name="CompanyName" type="xs:string" minOccurs="0" />               <xs:element name="ContactName" type="xs:string" minOccurs="0" />               <xs:element name="ContactTitle" type="xs:string" minOccurs="0"/>               <xs:element name="Address" type="xs:string" minOccurs="0" />               <xs:element name="City" type="xs:string" minOccurs="0" />               <xs:element name="Region" type="xs:string" minOccurs="0" />               <xs:element name="PostalCode" type="xs:string" minOccurs="0"/>               <xs:element name="Country" type="xs:string" minOccurs="0" />               <xs:element name="Phone" type="xs:string" minOccurs="0" />               <xs:element name="Fax" type="xs:string" minOccurs="0" />             </xs:sequence>           </xs:complexType>         </xs:element>         <xs:element name="Orders">           <xs:complexType>             <xs:sequence>               <xs:element name="OrderID" type="xs:int" minOccurs="0" />               <xs:element name="CustomerID" type="xs:string" minOccurs="0" />               <xs:element name="EmployeeID" type="xs:int" minOccurs="0" />               <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />               <xs:element name="RequiredDate" type="xs:dateTime" minOccurs="0"/>               <xs:element name="ShippedDate" type="xs:dateTime" minOccurs="0" />               <xs:element name="ShipVia" type="xs:int" minOccurs="0" />               <xs:element name="Freight" type="xs:decimal" minOccurs="0" />               <xs:element name="ShipName" type="xs:string" minOccurs="0" />               <xs:element name="ShipAddress" type="xs:string" minOccurs="0"/>               <xs:element name="ShipCity" type="xs:string" minOccurs="0" />               <xs:element name="ShipRegion" type="xs:string" minOccurs="0" />               <xs:element name="ShipPostalCode" type="xs:string" minOccurs="0"/>               <xs:element name="ShipCountry" type="xs:string" minOccurs="0" />             </xs:sequence>           </xs:complexType>         </xs:element>       </xs:choice>     </xs:complexType>   </xs:element> </xs:schema> 
Figure 46.6. The relationship between the Customers and Orders tables.

graphics/46fig06.jpg

The .NET Framework SDK supplies the XSD.exe (XML Schema Definition) tool. The XSD tool can generate XML Schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly. You simply provide the XML Schema file as input ( Customers.xsd in this example) along with a few directives. The file extensions drive the XSD tool logic. Thus, if you specify (and provide) an XML file, XSD.exe will infer a schema from the data in the file and produce an associated schema file ( .xsd ). If you specify (and provide) an XSD file (schema file), XSD.exe will generate source code for runtime objects that correspond to the XML Schema.

NOTE

To generate a typed DataSet that can be used by many programs (and languages such as VB and C#), you must start with an XML Schema representation of the DataSet . This will be the .xsd (XML Schema Definition) XML file. After you have created this, you can turn it into a typed DataSet and make it available to programs that reference it properly (via a generated .dll ). This .xsd schema of the DataSet must be compliant with the XML Schema definition language standards, available at http://www.w3.org/2001/XMLSchema and http://w3c.org.



Microsoft SQL Server 2000 Unleashed
Microsoft SQL Server 2000 Unleashed (2nd Edition)
ISBN: 0672324679
EAN: 2147483647
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net