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"
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>