home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / EditingXmlDocument.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  7.1 KB  |  169 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...Edit XML with the XmlNavigator?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to edit XML documents with an XmlNavigator using the DocumentNavigator and DataDocumentNavigator classes.
  9. This sample follows on from the <a href="NavigateXmlDocument.aspx">How Do I...Navigate XML with the XmlNavigator?</a> topic.
  10. </div>
  11.  
  12. <h4>Editing XML with the XmlNavigator</h4> 
  13. <Acme:SourceRef
  14. ViewSource="/quickstart/howto/samples/Xml/EditingXmlDocument/EditingXmlDocument.src"
  15. RunSample="/quickstart/howto/samples/Xml/EditingXmlDocument/EditingXmlDocument.aspx" 
  16. Icon = "/quickstart/images/genicon.gif"
  17. Caption="EditingXmlDocument.aspx"
  18. runat="server" />
  19.  
  20. <br clear="left"><br>
  21. <div class="indent" style="width:660">
  22. Here we are going to use the editing methods on the XmlNavigator to edit the XML document. First, using an XPath expression, all the books prices are selected in the 
  23. bookstore and the prices of the books are increased by 2%. Second a new book is added to the bookstore. The sample code shows the XPath expression. </div>
  24.  
  25. <div class="code">
  26. <xmp>
  27. navigator.Select ("descendant::book/price");
  28.  
  29. while (navigator.MoveToNextSelected())
  30. {
  31.     navigator.MoveToFirstChild();
  32.     Double price = Double.Parse(navigator.Value);
  33.     navigator.Value = Double.Format(price * 1.02, "#.00");
  34.     navigator.MoveToParent();
  35. }
  36. </xmp>
  37. </div>
  38.  
  39. <div class="indent" style="width:660">
  40. Here we move to the first text node (the book's price) with the <b>MoveToFirstChild</b> method, update it's value and then move back to the parent node with
  41. the <b>MoveToParent</b> method. The formatting on the string ensures that only two decimal places are written.</div>
  42.  
  43. <br clear="left"><div class="indent" style="width:660">
  44. Next we are going to insert a new book into the bookstore using the <b>Insert</b> method. This method creates a new node and inserts it at the specified
  45. position in the tree relative to the current position and moves the current node to point at this newly inserted node.</div>
  46.  
  47. <br clear="left"><div class="indent" style="width:660">
  48. The relative position is determined by the TreePosition enumeration shown in the table below.</div>
  49.  
  50. <br clear="left"><br>
  51. <DIV class=indent style="width:660">
  52. <TABLE class=table 
  53. style="border-style: solid"
  54. width="418">
  55. <TBODY>
  56. <TR>
  57. <TH width="100">TreePosition Enum</TH>
  58. <TH width="308">Description</TH>
  59. <TH width="10">Value</TH>
  60. </TR>
  61.   <tr>
  62.     <td height="17"><font size="1">None</font></td>
  63.     <td height="17"><font size="1">No position</font></td>
  64.     <td height="17"><font size="1">0</font></td>
  65.   </tr>
  66.   <tr>
  67.     <td width="100" height="19"><font size="1">Before</font></td>
  68.     <td width="308" height="19"><font size="1">The sibling immediately before the current node</font></td>
  69.     <td width="10" height="17"><font size="1">1</font></td>
  70.   </tr>
  71.   <tr>
  72.     <td width="100" height="19"><font size="1">After</font></td>
  73.     <td width="308" height="19"><font size="1">The sibling immediately after the current node</font></td>
  74.     <td width="10" height="17"><font size="1">2</font></td>
  75.   </tr>
  76.   <tr>
  77.     <td width="100" height="19"><font size="1">FirstChild</font></td>
  78.     <td width="308" height="19"><font size="1">The first child of the current node</font></td>
  79.     <td width="10" height="17"><font size="1">3</font></td>
  80.   </tr>
  81.   <tr>
  82.     <td width="100" height="19"><font size="1">LastChild</font></td>
  83.     <td width="308" height="19"><font size="1">The last child of the current node</font></td>
  84.     <td width="10" height="17"><font size="1">4</font></td>
  85.   </tr>
  86.   <tr>
  87.     <td width="100" height="19"><font size="1">Parent</font></td>
  88.     <td width="308" height="19"><font size="1">The parent of the current node</font></td>
  89.     <td width="10" height="17"><font size="1">5</font></td>
  90.   </tr>
  91.   <tr>
  92.     <td width="100" height="19"><font size="1">FirstAttribute</font></td>
  93.     <td width="1000" height="19"><font size="1">The first attribute of the current node</font></td>
  94.     <td width="10" height="17"><font size="1">6</font></td>
  95.   </tr>
  96.   <tr>
  97.     <td width="100" height="19"><font size="1">LastAttribute</font></td>
  98.     <td width="1000" height="19"><font size="1">The last attribute of the current node</font></td>
  99.     <td width="10" height="17"><font size="1">7</font></td>
  100.   </tr>
  101.   </TBODY></TABLE></DIV>
  102.   
  103. <br clear="left"><br><div class="indent" style="width:660">
  104. The code sample below starts at the document node and moves thorough the XML tree inserting element and attribute nodes to insert a new book into the XML document.
  105. Once updated the XML data is written out to a new file called 
  106. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/EditingXmlDocument/&file=updatebooks.xml">updatebooks.xml</a>.
  107.  
  108. <div class="code">
  109. <xmp>
  110. // Navigate to add a new book at the start of the bookstore
  111. navigator.MoveToDocumentElement();
  112.  
  113. // Insert the new book
  114. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Element,"book","","");
  115. navigator.SetAttribute ("genre","Object-Orientated Technology");
  116. navigator.SetAttribute ("publicationdate","1994");
  117. navigator.SetAttribute ("ISBN","0-201-63361-2");
  118. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Element,"title","","");
  119. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"title","","");
  120. navigator.Value = "Design Patterns - Elements of Reusable Object-Orientated Software";
  121. navigator.MoveToParent(); //author element node
  122. navigator.Insert(TreePosition.After,XmlNodeType.Element,"author","","");
  123. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Element,"first-name","","");
  124. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"first-name","","");
  125. navigator.Value = "Eric";
  126. navigator.MoveToParent(); //author element node
  127. navigator.Insert(TreePosition.After,XmlNodeType.Element,"last-name","","");
  128. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"last-name","","");
  129. navigator.Value = "Gamma";
  130. navigator.MoveToParent(); //author element node
  131. navigator.MoveToParent(); //title element node
  132. navigator.Insert(TreePosition.After,XmlNodeType.Element,"price","","");
  133. navigator.Insert(TreePosition.FirstChild,XmlNodeType.Text,"price","","");
  134. navigator.Value = "49.95";
  135. </xmp>
  136. </div>
  137.  
  138. <div class="indent" style="width:660">
  139. Finally the changes are written to a file.
  140. </div>
  141.  
  142. <div class="code">
  143. <xmp>
  144. xmldocument.Save(m_UpdatedDocument);
  145. </xmp>
  146. </div>
  147.  
  148. <H4>Summary</H4>
  149. <OL>
  150. <LI>The XmlNavigator provides a cursor style model to navigate an XML in-memory tree. 
  151. <LI>The Select method applies the XPath query to the XML document. This is the best method for retrieving a set of nodes to perform editing operations on.
  152. <LI>Insert and Remove methods allow you to perform editing operations on the XML document. 
  153. <LI>Editing operations can also be performed with the W3C DOM, however the use of XPath queries with the XmlNavigator makes this a more intuitive approach. 
  154. </LI></OL>
  155.  
  156. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.