Danh mục

Synchronizing a DataSet with an XML Document

Số trang: 9      Loại file: pdf      Dung lượng: 22.58 KB      Lượt xem: 4      Lượt tải: 0    
Thư viện của tui

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

Thông tin tài liệu:

[ Team LiB ] Recipe 8.3 Synchronizing a DataSet with an XML Document Problem You need to work with both a DataSet and its XML representation. Solution Use a synchronized DataSet and XmlDataDocument. The sample code contains two event handlers and one method:
Nội dung trích xuất từ tài liệu:
Synchronizing a DataSet with an XML Document [ Team LiB ]Recipe 8.3 Synchronizing a DataSet with an XML DocumentProblemYou need to work with both a DataSet and its XML representation.SolutionUse a synchronized DataSet and XmlDataDocument.The sample code contains two event handlers and one method:Go Button.Click Synchronizes a DataSet and an XmlDataDocument using one of three methods specified by the user. The default view for the Orders table of the DataSet is bound to the data grid on the form and the contents of the XmlDataDocument are displayed in the text box.Clear Button.Click Clears the contents of the data grid displaying the DataSet and the text box displaying the contents of the XmlDataDocument.FillDataSet( ) This method loads the DataSet with a subset of the Orders and Order Details data from Northwind and creates a relation between the tables.The C# code is shown in Example 8-5.Example 8-5. File: SyncDataSetWithXmlDocForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.Xml;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String ORDERS_TABLE = Orders;private const String ORDERDETAILS_TABLE = OrderDetails;// Relation name constantsprivate const String ORDERS_ORDERDETAILS_RELATION = Orders_OrderDetails_Relation;// Field name constantsprivate const String ORDERID_FIELD = OrderID;private const String XMLFILENAME = ConfigurationSettings.AppSettings[Project_Directory] + @Chapter 08\Orders_OrderDetails.xml;// . . .private void goButton_Click(object sender, System.EventArgs e){ Cursor.Current = Cursors.WaitCursor; DataSet ds = null; XmlDataDocument xmlDoc = null; if (method1RadioButton.Checked) { // Load DataSet with schema and data. ds = FillDataSet(true); // Get the XML document for the DataSet. xmlDoc = new XmlDataDocument(ds); } else if(method2RadioButton.Checked) { // Create DataSet with schema, but no data. ds = FillDataSet(false); // Get the XML document for the DataSet. xmlDoc = new XmlDataDocument(ds); // Load the data into the XML document from the XML file. xmlDoc.Load(XMLFILENAME); } else if(method3RadioButton.Checked) { // Create an XML document. xmlDoc = new XmlDataDocument( ); // Get the DataSet for the XML document. ds = xmlDoc.DataSet; // Get schema for the DataSet from the XSD inline schema. ds.ReadXmlSchema(XMLFILENAME); // Load the data into the XML document from the XML file. xmlDoc.Load(XMLFILENAME); } // Display the XML data. resultTextBox.Text = xmlDoc.OuterXml; // Bind the DataSet to the grid. dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView; Cursor.Current = Cursors.Default;}private DataSet FillDataSet(bool includeData){ DataSet ds = new DataSet(Orders_OrderDetails); SqlDataAdapter da; // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.FillSchema(orderTable, SchemaType.Source); if (includeData) da.Fill(orderTable); ds.Tables.Add(orderTable); // Fill the OrderDetails table with schema and add it to the DataSet. da = new SqlDataAdapter(SELECT * FROM [Order Details], ConfigurationSettings.AppSettings[Sql_ConnectString]); DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE); da.FillSchema(orderDetailTable, SchemaType.Source); if (includeData) da.Fill(orderDetailTable); ds.Tables.Add(orderDetailTable); // Create a relation between the tables. ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true); return ds;}DiscussionThe .NET Framework allows real-time, synchronous access to both a DataSet and itsXML representation in an XmlDataDocument object. The synchronized DataSet andXmlDataDocument work with a single set of data.The solution shows three ways to synchronize a DataSet with an XmlDataDocument:Method 1 Populate a DataSet with both schema and data. Synchronize it with a new XmlDataDocument, initializing it with the DataSet in the constructor.Method 2 Populate a DataSet with a schema but no data. Synchronize it with a new XmlDataDocument, initializing it with the DataSet in the constructor. Load an XML document into the XmlDataDocument. The table and column names in the DataSet schema to be synchronized must match those in the XmlDataDocument.Method 3 Create a new XmlDataDocument and access its DataSet through the DataSet property. Populate the schema for the DataSet. In the example, the schema is read from the XSD inline schema in the XML document. If the XML document does not have an inline schema, it might be possible to infer the schema using the InferSchema( ) method. Otherwise, the entire DataSet schema must be defined programmatically. Next, load the XML document into the synchronized XmlDataDocument. The table and column names in the DataSet schema to be synchronized must match those in the XmlDataDocument.Example 8-6 shows the XML file used in this solution.Example 8-6. Orders with Order Details XML file, with schema ...

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