![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
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
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 ...
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ìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Writing and Reading XML Using a DataSet ObjectTài liệu liên quan:
-
52 trang 439 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 328 0 0 -
74 trang 309 0 0
-
96 trang 305 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 299 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 291 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 291 1 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 282 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 279 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 278 0 0