Loading XML into a DataSet


Loading XML into a DataSet

XML can be loaded into a DataSet from a variety of sources, such as files or streams. Loading XML data from different sources often means using different method overloads in the DataSet . The specifics of loading XML data from a variety of sources are described in the following subsections.

Loading from a File

To load an XML file into a DataSet , use the DataSet.ReadXml(string) method and pass in the name of the file to load. For example:

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

After the XML is loaded, the DataSet is populated as if the data had been entered programmatically. If the XML file does not describe a valid DataSet , then an exception is thrown. Typical scenarios that cause exceptions include these:

  • The file to load does not exist.

  • The file to load exists, but it cannot be opened due to a sharing violation.

  • The data loaded into the DataSet has a constraint violation.

  • The data loaded into the DataSet has a null value in a DataColumn that is forbidden to be null.

  • The data loaded into the DataSet violates a ForeignKeyConstraint .

Loading Data from a Stream

You can load XML data coming from a stream by calling the DataSet.ReadXml(XmlReader, XmlReadMode) method. The XmlReader you pass in must be instantiated by somehow attaching it to an existing, valid stream. 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, which is discussed in Chapter 5, "Network Connectivity with the .NET Compact Framework." The following subsections show specific examples of how to load XML data from a stream attached to a variety of sources:

Loading from a Stream Attached to a File

To load a stream attached to a file, create an XmlTextReader by passing in a StreamReader to the XmlTextReader constructor. Then pass the XmlTextReader into DataSet.ReadXml() . For example:

 
 C# System.Xml.XmlTextReader l_XmlTextReader = new System.Xml.XmlTextReader(new         System.IO.StreamReader("\DataSet.xml")); l_DataSet.ReadXml(l_XmlTextReader, XmlReadMode.ReadSchema); l_XmlTextReader.Close(); VB Dim l_XmlTextReader as System.Xml.XmlTextReader = new System.Xml.XmlTextReader(new         System.IO.StreamReader("\DataSet.xml"); l_DataSet.ReadXml(l_XmlTextReader, XmlReadMode.ReadSchema) l_XmlTextReader.Close() 
Loading from a Stream Attached to a Memory Buffer

To load XML from 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. You might receive such a byte array via a TCP connection from a remote party, as discussed in Chapter 5.

  2. Instantiate a MemoryStream , passing the byte array from step 1 into the constructor.

  3. Instantiate a StreamReader on the MemoryStream from step 2.

  4. Instantiate an XmlTextReader on the StreamReader from step 3.

  5. Call DataSet.ReadXml() , passing in the XmlTextReader of step 4 as an argument.

The following code demonstrates these steps. It is assumed that l_ByteArray already holds memory into which XML text was written, so that it can be opened with a MemoryStream .

 
 C# // DataSet was written to memory stream l_OutMemStream // l_InBuff in an array of bytes l_InBuff = l_OutMemStream.GetBuffer(); DataSet l_MemStreamDataSet = new DataSet(); System.IO.MemoryStream l_InMemStream = new System.IO.MemoryStream         (l_InBuff, 0, 4000, false, true); l_XmlTextReader = new System.Xml.XmlTextReader(l_InMemStream); l_MemStreamDataSet.ReadXml(l_XmlTextReader, XmlReadMode.ReadSchema); l_XmlTextReader.Close(); l_InMemStream.Close(); VB ' DataSet was written to memory stream l_OutMemStream ' l_InBuff in an array of bytes l_InBuff = l_OutMemStream.GetBuffer() dim l_MemStreamDataSet as DataSet = new DataSet() dim l_InMemStream as System.IO.MemoryStream = new System.IO.MemoryStream         (l_InBuff, 0, 4000, False,         True) l_XmlTextReader = new System.Xml.XmlTextReader(l_InMemStream) l_MemStreamDataSet.ReadXml(l_XmlTextReader, XmlReadMode.ReadSchema) l_XmlTextReader.Close() l_InMemStream.Close() 
Loading XML from a Stream Associated with an Infrared Connection

You can pass a DataSet to another device via the device's infrared ports by using the IrDAClient class, which is discussed in Chapter 5. The IrDAClient class exposes the GetStream method, through which you can acquire a stream and transmit the XML description of a DataSet. To do so, 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 StreamReader by passing in the stream from step 2 into the StreamReader constructor.

  4. Create an XmlTextReader . Pass the StreamReader of step 3 into the XmlTextReader constructor.

  5. Pass the XmlTextReader into the DataSet.ReadXml() method.

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

 
 C# m_PhoneBookDataSet = new DataSet(); StreamReader l_StreamReader = null; XmlTextReader    l_XmlTextReader = null; l_StreamReader = new StreamReader(this.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII); l_XmlTextReader = new XmlTextReader(l_StreamReader); this.m_PhoneBookDataSet.ReadXml(l_XmlTextReader); l_XmlTextReader.Close(); VB Dim l_StreamReader As System.IO.StreamReader = Nothing Dim l_XmlTextReader As System.Xml.XmlTextReader = Nothing l_StreamReader = New System.IO.StreamReader(Me.m_IrDAClient.GetStream(),         System.Text.Encoding.ASCII) l_XmlTextReader = New System.Xml.XmlTextReader(l_StreamReader) Me.m_PhoneBookDataSet.ReadXml(l_XmlTextReader) l_XmlTextReader.Close() 

Inferring Schema

The XML data describing a DataSet can include schema. Described in detail later this chapter, schema is a way to explicitly describe the structure of the relational data that the DataSet holds. If the DataSet loads XML that does not explicitly describe the data layout with schema, then the DataSet structure is inferred by examining the DataRows described in the XML. You can force the DataSet to infer the schema, even if there is a schema section in the XML, by using the method overload DataSet.ReadXml(XmlReader, XmlReadMode.InferSchema) .



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