home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / SaveXmlDocument.aspx < prev    next >
Encoding:
Text File  |  2000-06-11  |  3.7 KB  |  94 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...Save XML with the XmlDocument (W3C DOM)?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to update and save XML with the XmlDocument (W3C DOM) class.<br clear="left"><br>
  9. This sample follows on from the <a href="DOMInterfaceXmlDocument.aspx">How do I...Load and use the XmlDocument (W3C DOM)?</a> topic.
  10. </div>
  11.  
  12. <h4>Saving XML with the XmlDocument</h4> 
  13. <Acme:SourceRef
  14. ViewSource="/quickstart/howto/samples/Xml/SaveXmlDocument/SaveXmlDocument.src"
  15. RunSample="/quickstart/howto/samples/Xml/SaveXmlDocument/SaveXmlDocument.aspx" 
  16. Icon = "/quickstart/images/genicon.gif"
  17. Caption="SaveXmlDocument.aspx"
  18. runat="server" />
  19.  
  20. <br clear="left"><br>
  21. <div class="indent" style="width:660">
  22. The XmlDocument class provides the ability to save XML data to files, streams and XmlWriters via the <b>Save</b> method. In this sample we are going to navigate over
  23. the sample file <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/SaveXmlDocument/&file=books.xml">books.xml</a>,
  24. increase all the books prices by 2% and save the XML to a new file called
  25. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/SaveXmlDocument/&file=updatebooks.xml">updatebooks.xml</a>
  26. as shown in the code below. </div>
  27.  
  28. <div class="code">
  29. <xmp>
  30. // Create XmlDocument and load the XML from file
  31. XmlDocument xmldocument = new XmlDocument();
  32. xmldocument.Load (new XmlTextReader (m_Document));
  33. Console.WriteLine ("XmlDocument loaded with XML data successfully ...");
  34.  
  35. IncreasePrice(xmldocument.DocumentElement);
  36.  
  37. // Write out data as XML
  38. xmldocument.Save(m_UpdatedDocument);
  39. Console.WriteLine();
  40. Console.WriteLine("Updated prices saved in file {0}", m_UpdatedDocument);
  41. </xmp></div>
  42.  
  43. <div class="indent" style="width:660">
  44. The IncreasePrice method recursively iterates over the XML document using the <b>FirstChild</b> method to move down the tree. This method returns null if
  45. there are no child nodes. 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.
  46. Every time a node named 'price' is encountered, the price is updated. The code below illustrates this.
  47. </div>
  48.  
  49. <div class="code">
  50. <xmp>
  51. public void IncreasePrice(XmlNode node)
  52. {       
  53.     if (node.Name == "price")
  54.     {
  55.         node = node.FirstChild;
  56.         Decimal price = Decimal.Parse(node.Value);
  57.         // Increase all the book prices by 2%
  58.         String newprice = Decimal.Format(price*(new Decimal(1.02)),"#.00");
  59.         Console.WriteLine("Old Price = " + node.Value + "\tNew price = " + newprice);
  60.         node.Value = newprice;
  61.     }
  62.  
  63.     node = node.FirstChild;
  64.     while (node != null)
  65.     {
  66.         IncreasePrice(node);
  67.         node = node.NextSibling;
  68.     }
  69. }
  70. </xmp>
  71. </div>
  72.  
  73. <div class="indent" style="width:660">
  74. Another useful property is the <b>HasChildNodes</b> property, that determines whether the current node has any children or not.
  75. The output from running this sample is shown below.</div>
  76.  
  77. <div class="code"><xmp>
  78. XmlDocument loaded with XML data successfully ...
  79.  
  80. Old Price = 8.99        New price = 9.17
  81. Old Price = 11.99       New price = 12.23
  82. Old Price = 9.99        New price = 10.19
  83.  
  84. Updated prices saved in file updatebooks.xml
  85. </xmp></div>
  86.  
  87. <H4>Summary</H4>
  88. <OL>
  89. <LI>The XmlDocument class can save XML to a file, stream or an XmlWriter.
  90. <LI>The XmlNode class enables you to navigate and amend the nodes within an XML document.
  91. </LI></OL>
  92.  
  93. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  94.