Integration of ADO. NET and XML

Source: Internet
Author: User

ADO. NET provides extensive XML support in dataset. Below are some tips for using XML and ADO. net.
I. dataset and XML
The perfect integration of dataset and XML allows you to do the following:
① Load a dataset plan or related structure from the XSD plan;
The following example shows the structure of an XSD file. mydataset is our dataset element. It contains a customers composite type element. With this element, we can map to create a table like this: MERs (customerid, companyName, phone) also defines the plan or structure of our Dataset:

 
<Xs: schema id = & quot; someid & quot;
Xmlns = & quot;
 
Xmlns: xs = & quot; http:// Www.w3.org/2001/xmlschema&quot;
Xmlns: msdata = & quot; URN: Schemas-Microsoft-com: XML-msdata & quot;>
 
<Xs: element name = & quot; mydataset & quot; msdata: isdataset = & quot;True& Quot;>
<Xs: complextype>
 
<Xs: Choice maxoccurs = & quot; unbounded & quot;>
<Xs: element name = & quot; customers & quot;>
 
<Xs: complextype>
<Xs: sequence>
 
<Xs: element name = & quot; customerid & quot; type = & quot; Xs: integer & quot; minoccurs = & quot; 0 & quot;/>
 
  
    string  & quot; minoccurs = & quot; 0 & quot;/> 
  
<Xs: element name = & quot; phone & quot; type = & quot; Xs:String& Quot;/>
 
</Xs: sequence>
</Xs: complextype>
 
</Xs: Element>
</Xs: Choice>
 
</Xs: complextype>
</Xs: Element>
 
</Xs: schema>

② Load a dataset content from the XML file;

To fill in the dataset content from an XML file, use the readxml method of the DataSet object. The following example shows how to read data from an XML file to a dataset:

 

Dataset myds = new dataset ();

Myds. readxml (& quot; input. xml & quot;, xmlreadmode. readschema );

③ Deduce a dataset plan from the content of an XML file when no plan is provided;

To load the dataset plan information from an XML file, you can use the readxmlschema method of the DataSet object. If no plan is provided, you can also use inferxmlschema to deduce the dataset plan from the XML file. The following example shows how to deduce the dataset plan from an XML file through inferxmlschema:

 

Dataset myds = new dataset ();

Myds. inferxmlschema (& quot; input_od.xml & quot;, new string [] & quot; URN: Schemas-Microsoft-com: officedata & quot ;);

④ Write a dataset plan like an XSD plan;

The following example shows how to load a dataset program from an XSD file through readxmlschema:

 

Dataset myds = new dataset ();

Myds. readxmlschema (& quot; schema. XSD & quot ;);

⑤ Read and write the content of a dataset like an XML file.

Using diffgrams to read and write content from dataset, the following example shows the result of updating a row of data in the table before submitting the change. The data row with customerid alfki is modified but not updated yet:

 

<Diffgr: DiffGram xmlns: msdata = & quot; URN: Schemas-Microsoft-com: XML-msdata & quot; xmlns: diffgr = & quot; URN: Schemas-Microsoft-com: xml-diffgram-v1 & quot;>

<Customerdataset>

<Customers diffgr: Id = & quot; customers1 & quot; msdata: roworder = & quot; 0 & quot; diffgr: haschanges = & quot; modified & quot;>

<Customerid> alfki </customerid>

<CompanyName> New Company </companyName>

</Customers>

<Customers diffgr: Id = & quot; customers2 & quot; msdata: roworder = & quot; 1 & quot; DiffGram: haserrors = & quot; true & quot;>

<Customerid> anrecognition </customerid>

<CompanyName> Ana Trujillo emparedados y helados </companyName>

</Customers>

<Customers diffgr: Id = & quot; customers3 & quot; msdata: roworder = & quot; 2 & quot;>

<Customerid> Anton </customerid>

<CompanyName> Antonio Moreno taquer í & amp; shy; A </companyName>

</Customers>

<Customers diffgr: Id = & quot; customers4 & quot; msdata: roworder = & quot; 3 & quot;>

<Customerid> arout </customerid>

<CompanyName> around the Horn </companyName>

</Customers>

</Customerdataset>

<Diffgr: Before>

<Customers diffgr: Id = & quot; customers1 & quot; msdata: roworder = & quot; 0 & quot;>

<Customerid> alfki </customerid>

<CompanyName> Alfreds futterkiste </companyName>

</Customers>

</Diffgr: Before>

<Diffgr: errors>

<Customers diffgr: Id = & quot; customers2 & quot; diffgr: Error = & quot; An Optimistic Concurrency violation has occurred for this row. & quot;/>

</Diffgr: errors>

</Diffgr: DiffGram>

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.