Saving a DataSet as XML


Saving a DataSet as XML

A DataSet can be written as XML to variety of outputs, such as files or streams. Saving a DataSet as XML data to different output types often means using different method overloads in the DataSet . The specifics of saving a DataSet as XML data to various outputs are described in the following subsections.

Saving to a File

To save a DataSet in XML format to a file, call the DataSet.WriteXml() method, for example, like so:

 
 C# l_DataSet.WriteXml("\MyDS.xml"); VB l_DataSet.WriteXDml("\MyDS.xml") 

WriteXml() throws an exception if there is a problem writing to the file. This is most typically caused by a sharing violation.

Saving Data to a Stream

You can save XML data to a stream by calling the DataSet.WriteXml(XmlWriter, XmlWriteMode) method. The XmlWriter you pass in must already be instantiated . That is, it must be constructed and an existing, valid stream must have been passed to the XmlWriter constructor. The stream in turn can be created by opening a file, using a memory buffer, or even accessing the stream from an infrared connection using the IrDAClient class. The following subsections show specific examples of how to write XML data to a stream attached to a variety of sources:

Saving to a Stream Attached to a File

To save to a stream attached to a file, follow these steps:

  1. Create a new instance of a System.IO.StreamWriter. Pass in the name of the XML file to write to.

  2. Acquire an XmlTextWriter . Pass in the StreamWriter of step 1 to the XmlTextWriter constructor.

  3. Call DataSet.WriteXml(XmlTextWriter, XmlWriteMode) . Pass in the XmlTextWriter of step 2.

For example:

 
 C# l_XmlTextWriter = new System.Xml.XmlTextWriter(new System.IO.StreamWriter         ("\DataSet.xml")); l_DataSet.WriteXml(l_XmlTextWriter, XmlWriteMode.WriteSchema); l_XmlTextWriter.Close(); VB l_XmlTextWriter = new System.Xml.XmlTextWriter(new System.IO.StreamWriter         ("\DataSet.xml")) l_DataSet.WriteXml(l_XmlTextWriter, XmlWriteMode.WriteSchema) l_XmlTextWriter.Close() 
Saving to a Stream Attached to a Memory Buffer

To save XML to a stream attached to a memory buffer, follow these steps:

  1. Acquire a reference to the byte array that will have a stream opened against it. Make sure the buffer is large enough to accommodate the data you plan to write.

  2. Instantiate a MemoryStream . Pass the byte array into the constructor.

  3. Instantiate a StreamWriter on the memory stream from step 2.

  4. Instantiate an XmlTextWriter on the StreamWriter from step 3.

  5. Call DataSet.WriteXml() . Pass in the XmlTextWriter of step 4 as an argument.

The following code demonstrates these steps:

 
 C# // l_OutBuff is a byte array large enough to hold the XML System.IO.MemoryStream l_OutMemStream = new System.IO.MemoryStream(l_OutBuff,         0, 4000, true, true); l_XmlTextWriter = new System.Xml.XmlTextWriter(l_OutMemStream,         System.Text.Encoding.Default); l_DataSet.WriteXml(l_XmlTextWriter, XmlWriteMode.IgnoreSchema); l_XmlTextWriter.Close(); l_OutMemStream.Close(); VB ' l_OutBuff is a byte array large enough to hold the XML Dim l_OutMemStream as System.IO.MemoryStream = new System.IO.MemoryStream(         l_OutBuff, 0, 4000, true, true) l_XmlTextWriter = new System.Xml.XmlTextWriter(l_OutMemStream,         System.Text.Encoding.Default) l_DataSet.WriteXml(l_XmlTextWriter, XmlWriteMode.IgnoreSchema) l_XmlTextWriter.Close() l_OutMemStream.Close() 
Writing XML to a Stream Associated with an Infrared Connection

The IrDAClient class, discussed in Chapter 5, provides access to an underlying stream by calling the GetStream() method. You can use this feature to pass a DataSet to another device connected by an IR link. To do so, as the party who is writing out a DataSet , follow these steps:

  1. Acquire an IrDAClient that is connected to another device. This process is described in detail in Chapter 5.

  2. Acquire a handle to the underlying stream by calling IrDAClient.GetStream() .

  3. Create a StreamWriter by passing in the stream from step 2 into the StreamWriter constructor.

  4. Create an XmlTextWriter . Pass the StreamWriter of step 3 into the XmlTextWriter constructor.

  5. Pass the XmlTextWriter into the DataSet.WriteXml() method.

The following code, borrowed from the XML_PhoneBook sample application, demonstrates these steps:

 
 C# StreamWriter l_StreamWriter = new StreamWriter(this.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII); System.Xml.XmlTextWriter l_XmlTextWriter = new System.Xml.XmlTextWriter         (l_StreamWriter); this.m_PhoneBookDataSet.WriteXml(l_XmlTextWriter, XmlWriteMode.WriteSchema); l_XmlTextWriter.Flush(); l_XmlTextWriter.Close(); VB Dim l_StreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter         (Me.m_IrDAClient.GetStream(), System.Text.Encoding.ASCII) Dim l_XmlTextWriter As System.Xml.XmlTextWriter =         New System.Xml.XmlTextWriter(l_StreamWriter) Me.m_PhoneBookDataSet.WriteXml(l_XmlTextWriter, XmlWriteMode.WriteSchema) l_XmlTextWriter.Flush() l_XmlTextWriter.Close() 

Writing Schema with the XML Data

You can control whether a DataSet writes schema information with the XML by using the DataSet.WriteXml(XmlWriter, XmlWriteMode) overload. Pass XmlWriteMode.WriteXmlSchema to force the DataSet to write schema information with the data or pass XmlWriteMode.IgnoreSchema to prevent the DataSet from writing schema information. XML Schema as it relates to DataSet s is discussed in greater detail later in this chapter.

The XmlWriteMode has the following values:

WriteSchema Write the schema when writing the DataSet as XML.

IgnoreSchema Ignore the schema when writing the DataSet as XML.

DiffGram Write the DataSet in XML DiffGram format. DiffGram format is discussed in greater detail later in this chapter.

WHEN TO WRITE XML SCHEMA

If you don't know who will load your XML-serialized DataSet , then it is generally a good idea to write schema with your XML. It provides consumers of the XML data an exact description of the relational data structure. Even though both the .NET Desktop Framework and the .NET Compact Framework DataSet can infer schema, software trying to consume your XML but not based on .NET technology might not be able to infer schema.


If the DataSet that will ultimately load the XML data already knows what the data schema is, then there is no need to write the schema with the XML. It would be wasteful and redundant.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

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