Writing and Reading XML Using a DataSet Object

Chia sẻ: Tuan Nghia | Ngày: | Loại File: PDF | Số trang:8

0
85
lượt xem
10
download

Writing and Reading XML Using a DataSet Object

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Writing and Reading XML Using a DataSet Object

  1. 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. The XML file written by this method contains the DataTable column names and values. You can write out the schema of a DataSet object to an XML file using the WriteXmlSchema() method. The XML file written by this method contains the structure of the DataTable objects contained in the DataSet. You can also get the XML in a DataSet 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 using the ReadXml() method. You can also read the schema contained in an XML file using the ReadXmlSchema() method. Note SQL Server also contains extensive built-in XML functionality, which you'll learn about in Chapter 16, "Using SQL Server's XML Support." Using the WriteXml() Method Let's say you have a DataSet object named myDataSet. Assume that myDataSet has a DataTable that contains the CustomerID, CompanyName, ContactName, and Address columns 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");
  2. 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 Customers table. 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.XmlWriteMode enumeration that specifies how to write XML data and the schema. Table 10.8 shows the constants defined in the XmlWriteMode enumeration. Table 10.8: XmlWriteMode ENUMERATION MEMBERS CONSTANT DESCRIPTION DiffGram Writes out the DataSet as a DiffGram, which contains the original values
  3. Table 10.8: XmlWriteMode ENUMERATION MEMBERS CONSTANT 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
  4. 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 the original SELECT statement, as well as the column values for the rows retrieved. Using the WriteXmlSchema() Method You 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
  5. As you can see, this file contains the schema definition for the columns retrieved from the Customers table by the original SELECT statement. Using the ReadXml() Method You 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 DataTable objects of the DataSet. For example, the following statement uses the ReadXml() method to 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 enumeration that specifies how to read XML data and the schema. Table 10.9 shows the constants defined in the XmlReadMode enumeration. Table 10.9: XmlReadMode ENUMERATION MEMBERS CONSTANT DESCRIPTION Auto Reads the XML file in an appropriate manner: • If the XML file contains a DiffGram, then XmlReadMode is set to DiffGram. • If the DataSet already contains a schema or the XML file contains a schema, then XmlReadMode is set to ReadSchema. • If the DataSet doesn't contain a schema and the XML file doesn't
  6. Table 10.9: XmlReadMode ENUMERATION MEMBERS CONSTANT DESCRIPTION contain a schema, then XmlReadMode is set to InferSchema. Auto is the default. DiffGram Reads the XML file as a DiffGram, which contains the original values and the changes to those values to make them current. The changes are then applied to your DataSet. This is similar to calling the Merge() method of a DataSet in that changes from one DataSet are merged with another. Fragment Reads an XML file that contains inline XDR schema fragments such as those generated by executing SELECT statements containing FOR XML clauses. IgnoreSchema Reads out only the data in the DataSet, without reading the schema. InferSchema Infers the schema of the XML file by examining the data stored in it. ReadSchema Reads the schema from the XML file into the DataSet. The following example shows the use of the XmlReadMode.ReadSchema constant: myDataSet.ReadXml("myXmlFile2.xml", XmlReadMode.ReadSchema); Listing 10.12 illustrates how to write and read XML files using ADO.NET. Listing 10.12: WRITEANDREADXML.CS /* WriteAndReadXml.cs illustrates how to write and read XML files */ using System; using System.Data; using System.Data.SqlClient; class WriteAndReadXML { public static void Main() { SqlConnection mySqlConnection = new SqlConnection( "server=localhost;database=Northwind;uid=sa;pwd=sa" );
  7. 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(); // use the WriteXml() method to write the DataSet out to an // XML file Console.WriteLine("Writing rows out to an XML file named " + "myXmlFile.xml using the WriteXml() method"); myDataSet.WriteXml("myXmlFile.xml"); Console.WriteLine("Writing schema out to an XML file named " + "myXmlFile2.xml using the WriteXml() method"); myDataSet.WriteXml("myXmlFile2.xml", XmlWriteMode.WriteSchema); // use the WriteXmlSchema() method to write the schema of the // DataSet out to an XML file Console.WriteLine("Writing schema out to an XML file named " + "myXmlSchemaFile.xml using the WriteXmlSchema() method"); myDataSet.WriteXmlSchema("myXmlSchemaFile.xml"); // use the Clear() method to clear the current rows in the DataSet myDataSet.Clear(); // use the ReadXml() method to read the contents of the XML file // into the DataSet Console.WriteLine("Reading rows from myXmlFile.xml " + "using the ReadXml() method"); myDataSet.ReadXml("myXmlFile.xml"); DataTable myDataTable = myDataSet.Tables["Customers"]; foreach (DataRow myDataRow in myDataTable.Rows) { Console.WriteLine("CustomerID = " + myDataRow["CustomerID"]); Console.WriteLine("CompanyName = " + myDataRow["CompanyName"]);
  8. Console.WriteLine("ContactName = " + myDataRow["ContactName"]); Console.WriteLine("Address = " + myDataRow["Address"]); } } } The output from this program is as follows: Retrieving rows from the Customers table Writing rows out to an XML file named myXmlFile.xml using the WriteXml() method Writing schema out to an XML file named myXmlFile2.xml using the WriteXml() method Writing schema out to an XML file named myXmlSchemaFile.xml using the WriteXmlSchema() method Reading rows from myXmlFile.xml using the ReadXml() method CustomerID = ALFKI CompanyName = Alfreds Futterkiste ContactName = Maria Anders Address = Obere Str. 57 CustomerID = ANATR CompanyName = Ana Trujillo3 Emparedados y helados ContactName = Ana Trujillo Address = Avda. de la Constitución 2222
Đồng bộ tài khoản