Danh mục

Writing and Reading XML Using a DataSet Object

Số trang: 8      Loại file: pdf      Dung lượng: 36.94 KB      Lượt xem: 5      Lượt tải: 0    
thaipvcb

Hỗ trợ phí lưu trữ khi tải xuống: 1,000 VND Tải xuống file đầy đủ (8 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Writing and Reading XML Using a DataSet Object XML is a convenient format for moving information around. You can write out the contents of the DataTable objects contained in a DataSet to an XML file using the WriteXml() method
Nội dung trích xuất từ tài liệu:
Writing and Reading XML Using a DataSet ObjectWriting and Reading XML Using a DataSet ObjectXML is a convenient format for moving information around. You can write out thecontents of the DataTable objects contained in a DataSet to an XML file using theWriteXml() method. The XML file written by this method contains the DataTablecolumn names and values.You can write out the schema of a DataSet object to an XML file using theWriteXmlSchema() method. The XML file written by this method contains the structureof the DataTable objects contained in the DataSet. You can also get the XML in aDataSet using the GetXml() method, which returns the XML in a string.You can read the contents of the DataTable objects in an XML file into a DataSet usingthe ReadXml() method. You can also read the schema contained in an XML file using theReadXmlSchema() method.Note SQL Server also contains extensive built-in XML functionality, which youll learn about in Chapter 16, Using SQL Servers XML Support.Using the WriteXml() MethodLets say you have a DataSet object named myDataSet. Assume that myDataSet has aDataTable that contains the CustomerID, CompanyName, ContactName, and Addresscolumns for the top two rows from the Customers table. The following code shows this:SqlCommand mySqlCommand = mySqlConnection.CreateCommand();mySqlCommand.CommandText = SELECT TOP 2 CustomerID, CompanyName, ContactName, Address + FROM Customers + ORDER BY CustomerID;SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();mySqlDataAdapter.SelectCommand = mySqlCommand;DataSet myDataSet = new DataSet();mySqlConnection.Open();Console.WriteLine(Retrieving rows from the Customers table);mySqlDataAdapter.Fill(myDataSet, Customers);mySqlConnection.Close();You can write out the contents of myDataSet to an XML file using the WriteXml()method. For example:myDataSet.WriteXml(myXmlFile.xml);This writes an XML file named myXmlFile.xml, as shown in Listing 10.9.Listing 10.9: MYXMLFILE.XML ALFKI Alfreds Futterkiste Maria Anders Obere Str. 57 ANATR Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 As you can see, this file contains the columns for the rows retrieved from the Customerstable.The WriteXml() method is overloaded as follows:void WriteXml(Stream myStream);void WriteXml(string fileName);void WriteXml(TextWriter myTextWriter);void WriteXml(XmlWriter myXmlWriter);void WriteXml(stream myStream, XmlWriteMode myXmlWriteMode);void WriteXml(string fileName, XmlWriteMode myXmlWriteMode);void WriteXml(TextWriter myTextWriter, XmlWriteMode myXmlWriteMode);void WriteXml(XmlWriter myXmlWriter, XmlWriteMode myXmlWriteMode);where myXmlWriteMode is a constant from the System.Data.XmlWriteModeenumeration that specifies how to write XML data and the schema. Table 10.8 shows theconstants defined in the XmlWriteMode enumeration. Table 10.8: XmlWriteMode ENUMERATION MEMBERSCONSTANT DESCRIPTIONDiffGram Writes out the DataSet as a DiffGram, which contains the original values Table 10.8: XmlWriteMode ENUMERATION MEMBERSCONSTANT DESCRIPTION and the changes to those values to make them current. You can generate a DiffGram that contains only the changes by calling the GetChanges() method of your DataSet, and then call WriteXml().IgnoreSchema Writes out only the data in the DataSet, without writing the schema. IgnoreSchema is the default.WriteSchema Writes out the schema in the DataSet.The following example shows the use of the XmlWriteMode.WriteSchema constant:myDataSet.WriteXml(myXmlFile2.xml, XmlWriteMode.WriteSchema);This writes an XML file named myXmlFile2.xml, as shown in Listing 10.10.Listing 10.10: MYXMLFILE2.XML ALFKI Alfreds Futterkiste Maria Anders Obere Str. 57 ANATR Ana Trujillo3 Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 As you can see, this file contains the schema definition for the columns used in theoriginal SELECT statement, as well as the column values for the rows retrieved.Using the WriteXmlSchema() MethodYou can write out the schema of myDataSet to an XML file using the WriteXmlSchema()method. For example:myDataSet.WriteXmlSchema(myXmlSchemaFile.xml);This writes an XML file named myXmlSchemaFile.xml, as shown in Listing 10.11.Listing 10.11: MYXMLSCHEMAFILE.XML As you can see, this file contains the schema definition for the columns retrieved from theCustomers table by the original SELECT statement.Using the ReadXml() MethodYou can read the contents of an XML file into a DataSet object using the ReadXml()method. This method reads the rows and columns from the XML file into DataTableobjects of the DataSet. For example, the following statement uses the ReadXml() methodto read the XML file myXmlFile.xml previously written by the WriteXml() method:myDataSet.ReadXml(myXmlFile.xml);The ReadXml() method is overloaded as follows:void ReadXml(Stream myStream);void ReadXml(string fileName);void ReadXml(TextReader myTextReader);void ReadXml(XmlReader myXmlReader);void ReadXml(stream myStream, XmlReadMode myXmlReadMode);void ReadXml(string fileName, XmlReadMode myXmlReadMode);void ReadXml(TextReader myTextReader, XmlReadMode myXmlReadMode);void ReadXml(XmlReader myXmlReader, XmlReadMode myXmlReadMode);where myXmlReadMode is a constant from the System.Data.XmlReadMode enumerationthat specifies how to read XML data and the schema. Table 10.9 shows the constantsdefined in the XmlReadMode enumeration. Table 10.9: XmlReadMode ENUMERATION MEMBERSCONSTANT DESCRIPTIONAuto Reads the XML ...

Tài liệu được xem nhiều: