home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / DOMInterfaceXmlDocument.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  11.5 KB  |  359 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" -->
  4.  
  5. <h4>How Do I...Load and use the XmlDocument (W3C DOM)?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates the classes which implement the
  9. <A href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html">W3C Document object Model (DOM) level 1 Core</A></A> and the
  10. <A href="http://www.w3.org/TR/DOM-Level-2/core.html">Core DOM Level 2</A></A>. The DOM is an in-memory (cache) tree representation of an XML document and
  11. enables the navigation and editing of this document.</div>
  12.  
  13. <h4>Loading and using the XmlDocument</h4> 
  14. <Acme:SourceRef
  15. ViewSource="/quickstart/howto/samples/Xml/DOMInterfaceXmlDocument/DOMInterfaceXmlDocument.src"
  16. RunSample="/quickstart/howto/samples/Xml/DOMInterfaceXmlDocument/DOMInterfaceXmlDocument.aspx" 
  17. Icon = "/quickstart/images/genicon.gif"
  18. Caption="DOMInterfaceXmlDocument.aspx"
  19. runat="server" />
  20.  
  21. <br clear="left"><br>
  22. <div class="indent" style="width:660">
  23. There are a number of classes implemented as part of the W3C DOM. Of these the <b>XmlNode</b> provides methods and properties to manipulate a node. Many of the other W3C classes
  24. are specializations of the XmlNode class. The <b>XmlDocument</b> class represents the XML document and has a <b>Load</b> method to load the document from a file,
  25. stream or an XmlReader. The list of classes is given in the table below.</div>
  26.  
  27. <br clear="left"><br>
  28. <DIV class=indent>
  29. <TABLE class=table 
  30. style="border-style: solid" 
  31. width="350">
  32. <TBODY>
  33. <TR>
  34. <TH>Class</TH>
  35. <TH>Derived from</TH>
  36. </TR>
  37.   <tr>
  38.     <td width="179" height="19"><font size="1">XmlNode</font></td>
  39.     <td width="308" height="19"><font size="1">implements ICloneable, IEnumerable</font></td>
  40.   </tr>
  41.   <tr>
  42.     <td width="179" height="19"><font size="1">XmlDocument</font></td>
  43.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  44.   </tr>
  45.   <tr>
  46.     <td width="179" height="19"><font size="1">XmlElement</font></td>
  47.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  48.   </tr>
  49.   <tr>
  50.     <td width="179" height="19"><font size="1">XmlAttribute</font></td>
  51.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  52.   </tr>
  53.   <tr>
  54.     <td width="179" height="19"><font size="1">XmlCharacterData</font></td>
  55.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  56.   </tr>
  57.   <tr>
  58.     <td width="179" height="19"><font size="1">XmlText</font></td>
  59.     <td width="308" height="19"><font size="1">XmlCharacterData</font></td>
  60.   </tr>
  61.   <tr>
  62.     <td width="179" height="19"><font size="1">XmlComment</font></td>
  63.     <td width="308" height="19"><font size="1">XmlCharacterData</font></td>
  64.   </tr>
  65.   <tr>
  66.     <td width="179" height="19"><font size="1">XmlCDataSection</font></td>
  67.     <td width="308" height="19"><font size="1">XmlCharacterData</font></td>
  68.   </tr>
  69.   <tr>
  70.     <td width="179" height="19"><font size="1">XmlProcessingInstruction</font></td>
  71.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  72.   </tr>
  73.   <tr>
  74.     <td width="179" height="19"><font size="1">XmlNodeList</font></td>
  75.     <td width="308" height="19"><font size="1">IEnumerable</font></td>
  76.   </tr>
  77.   <tr>
  78.     <td width="179" height="19"><font size="1">XmlNamedNodeMap</font></td>
  79.     <td width="308" height="19"><font size="1">IEnumerable</font></td>
  80.   </tr>
  81.   <tr>
  82.     <td width="179" height="19"><font size="1">XmlAttributeCollection</font></td>
  83.     <td width="308" height="19"><font size="1">XmlNamedNodeMap</font></td>
  84.   </tr>
  85.   <tr>
  86.     <td width="179" height="19"><font size="1">XmlDocumentFragment</font></td>
  87.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  88.   </tr>
  89.   <tr>
  90.     <td width="179" height="19"><font size="1">XmlDocumentType</font></td>
  91.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  92.   </tr>
  93.   <tr>
  94.     <td width="179" height="19"><font size="1">XmlEntityReference</font></td>
  95.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  96.   </tr>
  97.   <tr>
  98.     <td width="179" height="19"><font size="1">XmlEntity</font></td>
  99.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  100.   </tr>
  101.   <tr>
  102.     <td width="179" height="19"><font size="1">XmlNotation</font></td>
  103.     <td width="308" height="19"><font size="1">XmlNode</font></td>
  104.   </tr>
  105.   <tr>
  106.     <td width="179" height="19"><font size="1">XmlImplementation</font></td>
  107.     <td width="308" height="19"><font size="1">Object</font></td>
  108.   </tr>
  109.   </TBODY></TABLE></DIV>
  110.  
  111. <br clear="left"><br>
  112. <div class="indent" style="width:660">
  113. The code illustrated below shows an XmlDocument being loaded from an XmlTextReader, and the contents of the document are then displayed.</div>
  114.  
  115. <div class="code">
  116. <xmp>
  117. XmlTextReader reader = null;
  118.  
  119. try
  120. {
  121.     // Load the XML from file
  122.     Console.WriteLine ("Reading file {0} ...", m_Document);
  123.     reader = new XmlTextReader (m_Document);
  124.     Console.WriteLine ("File {0} read sucessfully ...", m_Document);
  125.  
  126.     // Create and XmlDocument from the XmlTextReader
  127.     XmlDocument xmldocument = new XmlDocument();
  128.     xmldocument.Load (reader);
  129.     Console.WriteLine ("XmlDocument loaded with XML data successfully ...");
  130.  
  131.     // Process the supplied XML file
  132.     Console.WriteLine ("Processing ...");
  133.     Console.WriteLine ();
  134.  
  135.     // Start from the document Element
  136.     DisplayTree(xmldocument.DocumentElement);
  137. }
  138. catch (Exception e)
  139. {
  140.     Console.WriteLine ("Exception: {0}", e.ToString());
  141. }
  142.  
  143. finally
  144. {
  145.     if (reader != null)
  146.         reader.Close();
  147. }
  148. </xmp></div>
  149.  
  150. <div class="indent" style="width:660">
  151. To navigate the document, the DisplayTree method recursively iterates over the XmlDocument using the <b>HasChildNodes</b> and <b>FirstChild</b> properties
  152. to move down the tree.
  153. The <b>NextSibling</b> property moves to the node immediately next to the current node, returning null if there is no node to move to.
  154. </div>
  155.  
  156. <div class="code">
  157. <xmp>
  158. public void DisplayTree(XmlNode node)
  159. {       
  160.     if (node != null)
  161.         Format (node);
  162.  
  163.     if (node.HasChildNodes)
  164.     {
  165.         node = node.FirstChild;
  166.         while (node != null)
  167.         {
  168.             DisplayTree(node);
  169.             node = node.NextSibling;
  170.         }
  171.     }
  172. }
  173. </xmp></div>
  174.  
  175. <div class="indent" style="width:660">
  176. The <b>Name</b> property gives the name of the node, and the <b>Value</b> property its value depending on the type. 
  177. The table below shows the value returned for a given node type.</div>
  178.  
  179. <br clear="left"><br>
  180. <DIV class=indent>
  181. <TABLE class=table 
  182. style="border-style: solid" 
  183. width="350">
  184. <TBODY>
  185. <TR>
  186. <TH>Type</TH>
  187. <TH>Value</TH>
  188. </TR>
  189.   <tr>
  190.     <td width="179" height="19"><font size="1">Attribute</font></td>
  191.     <td width="308" height="19"><font size="1">Value of attribute</font></td>
  192.   </tr>
  193.   <tr>
  194.     <td width="179" height="19"><font size="1">CDATA</font></td>
  195.     <td width="308" height="19"><font size="1">Content of the CDATA Section</font></td>
  196.   </tr>
  197.   <tr>
  198.     <td width="179" height="19"><font size="1">Comment</font></td>
  199.     <td width="308" height="19"><font size="1">Content of the comment</font></td>
  200.   </tr>
  201.   <tr>
  202.     <td width="179" height="19"><font size="1">Document</font></td>
  203.     <td width="308" height="19"><font size="1">null</font></td>
  204.   </tr>
  205.   <tr>
  206.     <td width="179" height="19"><font size="1">DocumentFragment</font></td>
  207.     <td width="308" height="19"><font size="1">null</font></td>
  208.   </tr>
  209.   <tr>
  210.     <td width="179" height="19"><font size="1">DocumentType</font></td>
  211.     <td width="308" height="19"><font size="1">null</font></td>
  212.   </tr>
  213.   <tr>
  214.     <td width="179" height="19"><font size="1">Element</font></td>
  215.     <td width="308" height="19"><font size="1">null</font></td>
  216.   </tr>
  217.   <tr>
  218.     <td width="179" height="19"><font size="1">Entity</font></td>
  219.     <td width="308" height="19"><font size="1">null</font></td>
  220.   </tr>
  221.   <tr>
  222.     <td width="179" height="19"><font size="1">EntityReference</font></td>
  223.     <td width="308" height="19"><font size="1">null</font></td>
  224.   </tr>
  225.   <tr>
  226.     <td width="179" height="19"><font size="1">Notation</font></td>
  227.     <td width="308" height="19"><font size="1">null</font></td>
  228.   </tr>
  229.   <tr>
  230.     <td width="179" height="19"><font size="1">ProcessingInstruction</font></td>
  231.     <td width="308" height="19">    <font size="1">Entire content excluding the target</font></td>
  232.   </tr>
  233.   <tr>
  234.     <td width="179" height="19"><font size="1">Text</font></td>
  235.     <td width="308" height="19"><font size="1">Content of the text node</font></td>
  236.   </tr>
  237.   </TBODY></TABLE></DIV>
  238.   
  239. <br clear="left"><br><div class="indent" style="width:660">
  240. The Format method, shown below, display the details of the current node using the Name and Value properties. For <b>XmlElement</b> node types the <b>Attributes</b>
  241. property provides a list of attributes as a <b>XmlNamedNodeMap</b> class. This class contains <b>GetNamedItem</b> to retrieve an XmlNode specified by name and 
  242. <b>SetNamedItem</b> to add an XmlNode using it's Name property. It also implements IEnumerable in order to support the 'foreach' statement.</div>
  243.  
  244. <div class="code">
  245. <xmp>
  246. // Format the output
  247. private void Format (XmlNode node)
  248. {
  249.     if (!node.HasChildNodes)
  250.     {
  251.         Console.Write("\t" + node.Name + "<" + node.Value + ">\r\n");
  252.     }
  253.     else
  254.     {
  255.         Console.Write(node.Name);
  256.         if(XmlNodeType.Element == node.NodeType)
  257.         {
  258.             XmlNamedNodeMap map = node.Attributes;
  259.             foreach (XmlNode attrnode in map)
  260.                 Console.Write(" " + attrnode.Name + "<" + attrnode.Value  + "> ");
  261.         }
  262.         Console.WriteLine();
  263.     }
  264. }
  265. </xmp></div>
  266.  
  267. <div class="indent" style="width:660">
  268. The XmlNode class also has a <b>NodeType</b> property. See <A target=content href="ReadXMLFile.aspx">How Do I...Read XML from a file?</A> for a table of
  269. the different node types. The output from running this sample is shown below.
  270. </div>
  271.  
  272. <div class="code">
  273. <xmp>
  274. Reading file books.xml ...
  275. File books.xml read sucessfully ...
  276. XmlDocument loaded with XML data successfully ...
  277. Processing ...
  278.  
  279. bookstore
  280. book genre<autobiography>  publicationdate<1981>  ISBN<1-861003-11-0>
  281. title
  282.         #text<The Autobiography of Benjamin Franklin>
  283. author
  284. first-name
  285.         #text<Benjamin>
  286. last-name
  287.         #text<Franklin>
  288. price
  289.         #text<8.99>
  290. book genre<novel>  publicationdate<1967>  ISBN<0-201-63361-2>
  291. title
  292.         #text<The Confidence Man>
  293. author
  294. first-name
  295.         #text<Herman>
  296. last-name
  297.         #text<Melville>
  298. price
  299.         #text<11.99>
  300. book genre<philosophy>  publicationdate<1991>  ISBN<1-861001-57-6>
  301. title
  302.         #text<The Gorgias>
  303. author
  304. name
  305.         #text<Plato>
  306. price
  307.         #text<9.99>
  308. </xmp></div>
  309.                                            
  310. <H4>Summary</H4>
  311. <OL>
  312. <LI>The XmlDocument, XmlNode and other classes implement the <A href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html">W3C Document object Model level 1 Core</A></A>
  313. and the <A href="http://www.w3.org/TR/DOM-Level-2/core.html">Core DOM Level 2</A></A> specifications.
  314. <LI>The XmlDocument is an in-memory (cache) tree representation of an XML document.
  315. <LI>There are different node types specializing from XmlNode to enable you to manipulate the XML document.
  316. <LI>You can get faster, non-cached, forward only stream access to XML using the XmlTextReader and XmlTextWriter classes.
  317. </LI></OL>
  318.  
  319. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.