Converting the DataSet to XML

Using the XML methods of the DataSet is quite straightforward, as you'll see in the next example. This example uses two GridView controls on a page. The first DataSet is filled directly from the Employees table of the Northwind database. (The code isn't shown here because it's similar to what you've seen in the previous chapters.) The second DataSet is filled using XML.

Here's how it works: once the DataSet has been created, you can generate an XML schema file describing the structure of the DataSet and an XML file containing the contents of every row. The easiest approach is to use the WriteXmlSchema() and WriteXml() methods of the DataSet. These methods provide several overloads, including a version that lets you write data directly to a physical file. When you write the XML data, you can choose between several slightly different formats by specifying an XmlWriteMode. You can indicate that you want to save both the data and the schema in a single file (XmlWriteMode.WriteSchema), only the data (XmlWriteMode. IgnoreSchema), or the data with both the current and the original values (XmlWriteMode. DiffGram).

Here's the code that you need to save a DataSet to an XML file:

Dim xmlFile As String = Server.MapPath("Employees.xml") ds.WriteXml(xmlFile, XmlWriteMode.WriteSchema)

This code creates an Employees.xml file in the current folder.

Now you can perform the reverse step by creating a new DataSet object and filling it with the data contained in the XML file using the DataSet.ReadXml() method as follows:

Dim dsXml As New DataSet("Northwind") dsXml.ReadXml(xmlFile)

This completely rehydrates the DataSet, returning it to its original state. If you want to see the structure of the generated Employees.xml file, you can open it in Internet Explorer, as shown in Figure 14-16. Notice how the first part contains the schema that describes the structure of the table (name, type, and size of the fields), followed by the data itself.

The DataSet XML follows a predefined format with a few simple rules:

• The root document element is the DataSet.DataSetName (for example, Northwind).

• Each row in every table is contained in a separate element, using the name of the table. The example with one table means that there are multiple <Employees> elements.

• Every field in the row is contained as a separate tag in the table row tag. The value of the field is stored as text inside the tag.

Unfortunately, the DataSet doesn't make it possible for you to alter the overall structure. If you need to convert the DataSet to another form of XML, you need to manipulate it by using XSLT or by loading it into an XmlDocument object.

31 htt p://local host/C hapte r 12/Employees.xrnl - Microsoft Internet Explorer

III

mm

File Edit View Favorites Tools Help * £ Search

Favorites

9

http://localhost/Chapterl2/Employees.xml

-<?Xml version="1.0" standalone="yes"

*

- <NewDataSet>

• <xs! schema id="Nei>iDataSet" xmlns="" xmlns:xs="http://www.w3,org/200i/XMLSchema"

xmlris:rnsdata="ur,n:schemas-microsoft-com:Kml-msdata">

- <xs: element name="NewDataSet" msdata:IsDataSet="true">

- <x5:complexType>

- <xs;choice maxOccurs="unbounded">

- <xs!element name="Employeesn>

- < xs ! compIexType>

- <;xs: sequence >

<xs:element name="EmployeeID" type="xs:int" mm0ccurs="0" />

<xs:element name="TitleOfCourtesy" type="Hs:string" minOccurs="Ö"

<xs: element name="LastName" type="Hs:string" min0ccurs="0" /> <xs:e|ement name^'FirstName0 type="xs:string" minOccurs="G" />

</xs!sequence>

</xs! complexType >

</xs:element>

</%$ ! choice >

</xs! complexType >

</xs: element >

</xs ¡schema >

- < Employees s-

< EmpioyeelD > 1 </EmployeeID >

< Titl e Of Co u rte s y > M s. </Titl eOfCo u rte s y >

<LastName>Davolio</LastName>

<FirstName>Nancy</FirstName>

</Employees>

■ < Employees >■

< Employ eeID>2</EmployeeID>

<TitleOfCourtesy>Dr.</TitleOfCourtesy>

<LastName>Fuller</LastName>

< Fi rstN a me > A n dre w </Fi rstN a m e>

«/Employees >

■ <EmpJoyees3-

< Employ eeID>3</EmployeeID>

< Titl e Of C o u rte sy > M s. < /Titl e Of C o u rte s y >

<LastName>Leverling</LastName>

< Fi rstN a m e >J a n et< /FîrstN a m e>

</Employees>

_Ä!

¿1 Done

Local intranet

_

Figure 14-16. Examiningthe DataSetXML

0 0

Post a comment

  • Receive news updates via email from this site