How to read XML from xmldatadocument
This example illustrates how to create an xmlreader class that can read XML data stored in xmldatadocument. After reading the data, this example applies the transform to the data, and then reads the converted data from xmldatadocument.
Note: This example shows how to load XML data to the topic of xmldatadocument.
The xmldatadocument class serves as a cache in the memory of XML data. This class allows you to load relational data or XML data, and then operate on the data using eXtensible style sheet language conversion (XSLT.
The xmldatadocument class is also closely related to the dataset class. In fact, you can construct an xmldatadocument class to map it to a dataset. For more information about ing xmldatadocument to a dataset, see how to create a dataset ing from the XSD Architecture
|
VB streamxmldocument. aspx |
[Running example] | [View Source code] |
In this exampleCodeFirst, load the data to xmldatadocument, which contains the XML data in the books. xml file. Then, this example creates a transform object and loads the XSLT style table identity. XSL to this object. This style sheet copies all the XML from the root node.
Before converting data, the data format must support XML Path Language (XPath) queries. This example allows xmldatadocument to create an xpathnavigator on which node conversion can be applied. Now, by applying the xsltransform class to xpathnavigator, this example can convert data. The converted data is returned as xmlreader. The format function can be used to read XML data in xmldatadocument.
Note: For more information about the transform class, see how to apply XSLT to XML data.
Private const string document = "books. XML "; private const string newstylesheet =" identity. XSL "; public static void main () {string [] ARGs = {document, newstylesheet}; streamxmldocumentsample mystreamxmldocumentsample = new streamxmldocumentsample (); mystreamxmldocumentsample. run (ARGs);} public void run (string [] ARGs) {xmlreader myxmlreader = NULL; xmldatadocument myxmldatadocument = new xmldata Document (); your transform my‑transform = new your transform (); try {// load the XML from file console. writeline ("loading file {0 }... ", argS [0]); myxmldatadocument. load (ARGs [0]); console. writeline ("xmldatadocument loaded with XML data successfully... \ r \ n "); console. writeline ("reading XML... \ r \ n "); my‑transform. load (ARGs [1]); xpathnavigator myxpathnavigator = myxmldatadocument. createnavigat Or (); myxmlreader = mydomaintransform. transform (myxpathnavigator, null); formatxml (myxmlreader);} catch (exception e) {console. writeline ("exception: {0}", E. tostring ();} finally {// close the reader if (myxmlreader! = NULL) myxmlreader. Close ();}} Private const document as string = "books. XML "Private const newstylesheet as string =" identity. XSL "shared sub main () dim ARGs as string () = {document, newstylesheet} dim mystreamxmldocumentsample as streamxmldocumentsample mystreamxmldocumentsample = new streamxmldocumentsample () mystreamxmldocumentsample. run (ARGs) end sub public sub run (ARGs as string () dim myxmlreader as xmlreader = nothing dim ready as xmldatadocument = new xmldatadocument () dim my‑transform as new transform = new transform () try 'Load the XML from file console. writeline ("loading file {0 }... ", argS (0) myxmldatadocument. load (ARGs (0) console. writeline ("xmldatadocument loaded with XML data successfully... ") console. writeline ("reading XML... ") console. writeline () mydomaintransform. load (ARGs (1) dim myxpathnavigator as xpathnavigator = myxmldatadocument. createnavigator () myxmlreader = mydomaintransform. transform (myxpathnavigator, nothing) formatxml (myxmlreader) catch e as exception console. writeline ("exception: {0}", E. tostring () Finally 'close the reader if not myxmlreader is nothing myxmlreader. close () end if end try end sub |
C # |
VB |
|
The formatxml function is used to describe how to read XML from xmldatadocument. UseXmlreaderRead all the content of xmldatadocument and write the XML content in the custom format.
Private Static void formatxml (xmlreader reader) {While (reader. read () {Switch (reader. nodetype) {Case xmlnodetype. processinginstruction :...}}} // format the output Private Static void format (xmlreader reader, string nodetype) {// format the output ...} private sub formatxml (reader as xmlreader) while reader. read () Select (reader. nodetype) Case xmlnodetype. processinginstruction :... end select end while end sub private sub format (byref reader as xmlreader, nodetype as string) 'format the output... end sub |
C # |
VB |
|
Summary
- The xmldatadocument class enables you to load relational data or XML data.
- You can create an xmlreader for the XML data in xmldatadocument by using external transform.