Use .NET storage XML data (2)

xiaoxiao2021-03-06  49

Mapping XML order assumes that you are writing an application that accepts user orders, the order is XML format, and its XSD outline is defined in Figure 1. The outline defines three composite types, providing customer data, order data, and linear data items for orders, respectively. A top Customer element defines the root of an XML document. This closed system defines the relationship between elements: The Order element contains a lineItem element, and the Customer element contains an Order element. Figure 2 shows an XML document instance that complies with the outline defined by Figure 1.

Figure 1: XSD Outline

Figure 2: An XML document example Alfki 9572658

ONE Main Street Anywhere nj 08080 10966 37 26.50 8 gravad lax 56 38.00 12 Gnocchi di nonna alice

The C # code shown in Listing 1 uses the READXMLSCHEMA method to load the outline of Figure 1 into the data set called OrderDS. READXMLSCHEMA establishes three data sheets, which correspond to the Customer, Order, and LineItem elements defined in the outline. So you can verify that this outline has established an expected table in the relational data cache. The PrintSshape method writes the name of each table to the console, followed by the list of columns and the data type of each column.

Listing 1: Creating a C # code of relational data cache

using System; using System.Collections; using System.Data; using System.Data.SqlClient; using System.Xml; public class XMLMap {public static void Main () {// read outline data set and establish DataSet orderDS = new DataSet ("CustOrder"); ORDERDS.READXMLSCHEMA ("CustorderLitem.xsd"); // Print the form of the data set PrintDSSHAPE (ORDERDS); // Read the order of an XML format into the dataset order ORDERDS.READXML ("Order.xml" , System.Data.xmlreadMode.DataReschema); // Print data printdsdata (ORDERDS) in the data set; // Insert business rules and database update logic} private static void printdsshape (DataTable DT IN DS). Tables) {Console.WriteLine ("{0}", dt.tablename); // Print column name and type Foreach (Datacolumn DC in dt.columns) console.writeline ("/ t {0} / t {1} ", dc.columnname, dc.datatype.toString ());}} private static void printdsdata (DataTable DT in ds.tables) {Console.WriteLine (" / n {0}: ", DT .TABLENAME); // Printing the head of the column Foreach (Datacolumn DC in dt.columns) console.write ("{0} / t", dc.columnname; console.writeline (""); // Output Data Foreach ( DataRow Dr in Dt.ROWS) {Foreach (Datacolumn DC in Dt.columns) System.Console.write ("{0} / t", DR [DC]); SYST Em.console.writeLine ("");}}}} Take a closer look at the name. Although there is no Customer_ID and ORDER_ID columns in the outline, they still appear in the data table. READXMLSCHEMA automatically adds these columns to the dataset. The data set uses these columns as an external key to simulate the relationship between the Customer element between its Order elements, the ORDER element and its LineItem element. Because XML uses nested relationships in the typical case, the data set automatically generates its own primary key, the external key between the data tables, and stores them in these columns.

Carefully view the data type in Figure 3 - Data Sets have mapped data types from the XML outline data to the corresponding .NET data type. When you load the XML document into the data set, the data set converts each value from XML to the corresponding .NET type.

Figure 3: Generated data type and record

CustomerCustomerID System.StringCustomer_Id System.Int32OrderOrderID System.Int64Order_Id System.Int32Customer_Id System.Int32LineItemProductID System.Int32Quantity System.Int32UnitPrice System.DecimalOrder_Id System.Int32Customer: CustomerID Customer_IdALFKI 0Order: OrderID Order_Id Customer_Id10966 0 0LineItem: ProductID Quantity UnitPrice Order_Id37 8 26.5 056 12 38 0 put After the outline is loaded, in order to complete the relationship mapping, all things you need to do is to load XML data into the dataset. Listing 1 The READXML method opens the file called Order.xml, which is shown in Figure 2. Then, it reads the data in the file to the data sheet you just read the data set established by the outline. Your XML order can now be accessed through a dataset.

In order to demonstrate how to access data in the data set, the list 1 of the list 1 navigates in the data table. For each table, the name of the column is displayed, followed by displaying all the rows of this table. Figure 3 shows the Customer_ID and ORDER_ID columns added to the readxmlschema method to automatically generate values.

Note that the three elements of Order.xml --PO, Address, and Description - not mapped to the data table. These data is ignored because the outline you provide to the dataset does not contain these elements, and when the data set establishes the shape of the relational data cache and loads XML data, it simply ignores the data not described in the outline. Even in the XML order you have received from the customer contains uniserated additional data, this simple feature can also make your code work normally.

Establish an application using a data cache

Now you know how to use the dataset to establish a relational data cache for XML data, you can apply this technique to implement an application that performs business logic and updates SQL Server applications. The business logic is relatively straightforward when you use the data set programming model. ADO.NET provides you with several options for updating data in SQL Server, including using data adapters, writing your own queries, and performing stored procedures. Data Element Map the XML data into a relational model is easy, and the remaining things are yours.

转载请注明原文地址:https://www.9cbs.com/read-79037.html

New Post(0)