Example of synchronizing dataset with xmldatadocument (programming of nodes and fields)

Source: Internet
Author: User
Tags chop

Allows you to store, retrieve, and operate on structured data through related dataset.

For a list of all members of this type, see xmldatadocument members.

System. Object
System. xml. xmlnode
System. xml. xmldocument
System. xml. xmldatadocument

 

 
[C #]Public class xmldatadocument: xmldocument

 

 

 
[C ++]Public _ GC class xmldatadocument: Public xmldocument
Thread Security

All public static values of this type (in Visual BasicShared) Members are safe for multi-threaded operations. However, it is not guaranteed that any instance Member is thread-safe.

Remarks

This class extends xmldocument. It allows you to load relational or XML data and operate on the data using the W3C Document Object Model (DOM. Dom displays the data as a node object hierarchy. BecauseXmldatadocumentImplements the ixpathnavigable interface, so it can also be used as the source document of the xsltransform class.

XmldatadocumentAndDatasetThe class has a close subordination, and the latter provides a link view of the loaded XML data. PairXmldatadocumentAny changes made will be reflected inDatasetAnd vice versa.

To load XML dataDatasetUse readxmlschema to generate a link ing. Then, you can use load or loadxml to load XML data.

To load link dataDatasetIt is specified as a parameter in the xmldatadocument constructor.

Requirements

Namespace:System. xml

Platform:Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 Series

ProgramSet:System. Data (in system. Data. dll)

Initialize a new instance of the xmldatadocument class.
Reload list
Initialize a new instance of the xmldatadocument class.
[C #] public xmldatadocument ();
[C ++] public: xmldatadocument ();
Use the specified dataset to initialize a new instance of the xmldatadocument class.
[C #] public xmldatadocument (Dataset );
[C ++] public: xmldatadocument (Dataset *);
Example
[Visual Basic, C #] the following example loads a customer table to xmldatadocument.
[Visual Basic, C #] This example uses the SQL Server 2000 northwind database.
[Visual Basic, C #] Note that this example shows how to use an overloaded version of The xmldatadocument constructor. For other examples available, see separate overload topics.
[C #]
Using system;
Using system. Data;
Using system. xml;
Using system. Data. sqlclient;

Public class sample
{
Public static void main ()
{
Dataset dsnorthwind = new dataset ();

// Create the connection string.
String sconnect;
Sconnect = "Data Source = localhost; Integrated Security = sspi; initial catalog = northwind ";

// Create a connection object to connect to the northwind dB.
Sqlconnection nwconnect = new sqlconnection (sconnect );

// Create a command string to select all the MERs in the Wa region.
String scommand = "select * from MERs where region = 'wa '";

// Create an adapter to load the dataset.
Sqldataadapter mydataadapter = new sqldataadapter (scommand, nwconnect );

// Fill the dataset with the selected records.
Mydataadapter. Fill (dsnorthwind, "MERs ");

// Load the document with the dataset.
Xmldatadocument Doc = new xmldatadocument (dsnorthwind );

// Display the xmldatadocument.
Doc. Save (console. Out );

}
}

This section describes how to process an order.XmldatadocumentSync strong typeDataset. The subsequent example will createDataset, Which matches only part of the source XML document. These examples will useXmldatadocumentTo avoid source XML file distortion, so thatDatasetIt can be used to publish a subset of XML documents.

The following XML documents contain all order-related information: customer information, purchased goods, and shipping information.

<? XML version = "1.0" standalone = "yes"?> <Purchaseorder> <customers> <customerid> chops </customerid> <orders> <orderid> 10966 </orderid> <orderdetails> <orderid> 10966 </orderid> <productid> 37 </productid> <unitprice> 26 </unitprice> <quantity> 8 </quantity> <discount> 0 </discount> </orderdetails> <orderid> 10966 </orderid> <productid> 56 </productid> <unitprice> 38 </unitprice> <quantity> 12 </quantity> <discount> 0.15 </discount> </orderdetails> <orderdetails> <orderid> 10966 </orderid> <productid> 62 </productid> <unitprice> 49.3 </unitprice> <quantity> 12 </quantity> <discount> 0.15 </ discount> </orderdetails> <customerid> chops </customerid> <employeeid> 4 </employeeid> <orderdate> 1998-03-20t00: 00: 00.0000000 </orderdate> <requireddate> 1998-04-17t00: 00: 00.0000000 </requireddate> <shippeddate> 1998-04-08t00: 00: 00.0000000 </shippeddate> <shipvia> 1 </shipvia> <freight> 27.19 </freight> <shipname> chop-suey Chinese </shipname> <shipaddress> hauptstr. 31 </shipaddress> <shipcity> Bern </shipcity> <shippostalcode> 3012 </shippostalcode> <shipcountry> Switzerland </shipcountry> </orders> <companyName> chop-suey Chinese </companyName> <contactname> Yang Wang </contactname> <contacttitle> owner </contacttitle> <address> hauptstr. 29 </address> <city> Bern </city> <postalcode> 3012 </postalcode> <country> Switzerland </country> <phone> 0452-076545 </phone> </customers> <shippers> <shipperid> 1 </shipperid> <companyName> speedy express </companyName> <phone> (503) 555-9831 </phone> </shippers> <shipperid> 2 </shipperid> <companyName> United package </companyName> <phone> (503) 555-3199 </phone> </shippers> <shipperid> 3 </shipperid> <companyName> Federal shipping </companyName> <phone> (503) 555-9931 </phone> </shippers> <Products> <productid> 37 </productid> <productname> gravad lax </productname> <quantityperunit> 12-500g pkgs. </quantityperunit> <unitsinstock> 11 </unitsinstock> <unitsonorder> 50 </unitsonorder> <reorderlevel> 25 </reorderlevel> </Products> <productid> 56 </productid> <productname> gnocchi di Nonna Alice </productname> <quantityperunit> 24-250g pkgs. </quantityperunit> <unitsinstock> 21 </unitsinstock> <unitsonorder> 10 </unitsonorder> <reorderlevel> 30 </reorderlevel> </Products> <productid> 62 </productid> <productname> tarte au sure </productname> <quantityperunit> 48 pies </quantityperunit> <unitsinstock> 17 </unitsinstock> <unitsonorder> 0 </unitsonorder> <reorderlevel> 0 </reorderlevel> </Products> </purchaseorder>

 

When processing the order information contained in the preceding XML document, one of the steps is to use the company's current inventory to supply the order. Employees who supply orders from the company warehouse do not need to view all the content of the order. They only need to view the product information of the order. To publish product information only from XML documents, you can create a strong typeDataset,DatasetIt is written in the form of an XML Schema Definition Language (XSD) architecture and has a schema mapped to the purchased product and quantity. Strong typeDatasetFor more information about the object, see use a typed dataset.

BelowCodeDisplays the strong type generated for this example.Dataset.

<? XML version = "1.0" standalone = "yes"?> <Xs: schema id = "orderdetail" xmlns = "" xmlns: xs = "http://www.w3.org/2001/XMLSchema" xmlns: codegen = "urn: Schemas-Microsoft-com: XML-mspdrop" xmlns: msdata = "urn: Schemas-Microsoft-com: XML-msdata"> <Xs: element name = "orderdetail" msdata: isdataset = "true"> <Xs: complextype> <Xs: Choice maxoccurs = "unbounded"> <Xs: element name = "orderdetails" codegen: typedname = "lineitem" codegen: typedplural = "lineitems"> <Xs: complextype> <Xs: sequence> <Xs: element name = "orderid" type = "XS: int" minoccurs = "0" codegen: typedname = "orderid"/> <Xs: element name = "quantity" type = "XS: short" minoccurs = "0" codegen: typedname = "quantity"/> <Xs: element name = "productid" type = "XS: int" minoccurs = "0" codegen: typedname = "productid"/> </Xs: sequence> </Xs: complextype> </Xs: Element> <Xs: element name = "Products" codegen: typedname = "product" codegen: typedplural = "Products"> <Xs: complextype> <Xs: sequence> <Xs: element name = "productid" type = "XS: int" minoccurs = "0" codegen: typedname = "productid"/> <Xs: element name = "productname" type = "XS: string" minoccurs = "0" codegen: typedname = "productname"/> <Xs: element name = "quantityperunit" type = "XS: string" minoccurs = "0" codegen: typedname = "quantityperunit"/> <Xs: element name = "unitsinstock" type = "XS: short" minoccurs = "0" codegen: typedname = "unitsinstock"/> <Xs: element name = "unitsonorder" type = "XS: short" minoccurs = "0" codegen: typedname = "unitsonorder"/> <Xs: element name = "reorderlevel" type = "XS: short" minoccurs = "0" codegen: typedname = "reorderlevel"/> </Xs: sequence> </Xs: complextype> </Xs: Element> </Xs: Choice> </Xs: complextype> <Xs: Unique name = "constraint1"> <Xs: Selector XPath = ". // products "/> <Xs: Field XPath =" productid "/> </Xs: Unique> <Xs: keyref name =" relation1 "refer =" constraint1 "codegen: typedchildren = "getlineitems" codegen: typedparent = "product"> <Xs: Selector XPath = ". // orderdetails "/> <Xs: Field XPath =" productid "/> </Xs: keyref> </Xs: Element> </Xs: schema>

 

Please note that,DatasetOnlyOrderdetailsAndProductsElement Information. EnablingDatasetAndXmldatadocumentSynchronization to ensure that it is not included inDatasetKeep the elements in the XML document.

UseNorthwind. fillorder) Generate a strong typeDataset, You canDatasetAndXmldatadocumentSynchronize to expose part of the initial XML document. Note thatDatasetContains structure, but does not contain data. Load XMLXmldatadocumentThe data is entered. If you try to loadDatasetSynchronizedXmldatadocument.

When updatingDataset(AndXmldatadocument,XmldatadocumentYou can write the modified XML documentDatasetThe ignored elements remain unchanged (as shown below ). In the order example, after filling the order item, the modified XML document will be passed to the next step of the order process (may be passed to the company shipping department ).

 

  [C #]  using system; using system. data; using system. XML; using northwind. fillorder; public class sample {public static void main () {orderdetail orderds = new orderdetail (); xmldatadocument xmldoc = new xmldatadocument (orderds); xmldoc. load ("order. XML "); foreach (orderdetail. lineitem myitem in orderds. lineitems) {orderdetail. product myprod = myitem. product; // remove quantity from the current stock. myprod. unitsinstock = (short) (myprod. unitsinstock-myitem. quantity); // if the remaining stock is less than the reorder level, order more. if (myprod. unitsinstock + myprod. unitsonorder) 
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.