Saving a DataSet as XMLA 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 FileTo 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 StreamYou 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 FileTo save to a stream attached to a file, follow these steps:
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 BufferTo save XML to a stream attached to a memory buffer, follow these steps:
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 ConnectionThe 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:
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 DataYou 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:
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. |