home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / SaveDataSetXML.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  6.2 KB  |  182 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 a DataSet as XML?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to save relational data loaded into a DataSet to a file as XML using the XmlDataDocument. This demonstrates the transition between
  9. relationally mapped data and XML data.
  10. <br clear="left"><br>
  11. This example follows on from the <a href="/quickstart/howto/doc/Xml/SaveDataSetMapXSDSchema.aspx">How Do I...Save DataSet mappings to an XSD schema file?</a> topic.
  12. </div>
  13.  
  14. <h4>Saving a DataSet as XML</h4> 
  15. <Acme:SourceRef
  16. ViewSource="/quickstart/howto/samples/Xml/SaveDataSetXMLData/SaveDataSetXMLData.src"
  17. RunSample="/quickstart/howto/samples/Xml/SaveDataSetXMLData/SaveDataSetXMLData.aspx" 
  18. Icon = "/quickstart/images/genicon.gif"
  19. Caption="SaveDataSetXMLData.aspx"
  20. runat="server" />
  21.  
  22. <br clear="left"><br>
  23. <div class="indent" style="width:660">
  24. Having built the relational tables in the DataSet then we can now save these as XML data. This is the generation of hierarchical XML based upon and validated
  25. against the internally generated XSD schema. First an XmlDataDocument is created for the DataSet.
  26. </div>
  27.  
  28. <div class="code"><xmp>
  29. // Load the DataSet with relation data
  30. DataSet dataset = new DataSet();
  31. LoadDataSet(dataset);
  32. // Create an XmlDataDocument for the DataSet
  33. XmlDataDocument datadoc = new XmlDataDocument(dataset);
  34. </xmp></div>
  35.  
  36. <div class="indent" style="width:660">
  37. To save the XML to a file the XmlDataDocument <b>Save</b> method is called with a StreamWriter representing the file.
  38. </div>
  39.  
  40. <div class="code"><xmp>
  41. // Write data as XML
  42. internal void WriteXMLData(XmlDataDocument datadoc)
  43. {
  44.     StreamWriter writer = null;
  45.  
  46.     try
  47.     {
  48.         Console.WriteLine("Writing the XML data to {0} ...", m_NewDocument);
  49.         writer = new StreamWriter(m_NewDocument);
  50.         datadoc.Save(writer);
  51.     }
  52.  
  53.     catch (Exception e)
  54.     {
  55.         Console.WriteLine ("Exception: {0}", e.ToString());
  56.     }
  57.  
  58.     finally
  59.     {
  60.         if (writer != null)
  61.             writer.Close();
  62.     }
  63. }
  64. </xmp></div>
  65.  
  66. <div class="indent" style="width:660">
  67. The XML is written to the file
  68. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/SaveDataSetXMLData/&file=PersonPet.xml">PersonPet.xml</a>.
  69. The XML in the XmlDataDocument can now be read with an XmlReader and displayed, showing the XML for the relational data.
  70. See <A target=content href="StreamAnXmlDocument.aspx">How do I...Read XML from an XmlDataDocument?</A> The output from this is shown below.
  71. </div>
  72.  
  73. <div class="code"><xmp>
  74. Writing the XML data to PersonPet.xml ...
  75. Element<PersonPet>
  76.         Element<Person>
  77.                 Element<ID>
  78.                         Text<#text>0
  79.                 Element<Name>
  80.                         Text<#text>Mark
  81.                 Element<Age>
  82.                         Text<#text>18
  83.         Element<Person>
  84.                 Element<ID>
  85.                         Text<#text>1
  86.                 Element<Name>
  87.                         Text<#text>William
  88.                 Element<Age>
  89.                         Text<#text>12
  90.         Element<Person>
  91.                 Element<ID>
  92.                         Text<#text>2
  93.                 Element<Name>
  94.                         Text<#text>James
  95.                 Element<Age>
  96.                         Text<#text>7
  97.         Element<Person>
  98.                 Element<ID>
  99.                         Text<#text>3
  100.                 Element<Name>
  101.                         Text<#text>Levi
  102.                 Element<Age>
  103.                         Text<#text>4
  104.         Element<Pet>
  105.                 Element<ID>
  106.                         Text<#text>0
  107.                 Element<OwnerID>
  108.                         Text<#text>0
  109.                 Element<Name>
  110.                         Text<#text>Frank
  111.                 Element<Type>
  112.                         Text<#text>cat
  113.         Element<Pet>
  114.                 Element<ID>
  115.                         Text<#text>1
  116.                 Element<OwnerID>
  117.                         Text<#text>1
  118.                 Element<Name>
  119.                         Text<#text>Rex
  120.                 Element<Type>
  121.                         Text<#text>dog
  122.         Element<Pet>
  123.                 Element<ID>
  124.                         Text<#text>2
  125.                 Element<OwnerID>
  126.                         Text<#text>2
  127.                 Element<Name>
  128.                         Text<#text>Cottontail
  129.                 Element<Type>
  130.                         Text<#text>rabbit
  131.         Element<Pet>
  132.                 Element<ID>
  133.                         Text<#text>3
  134.                 Element<OwnerID>
  135.                         Text<#text>3
  136.                 Element<Name>
  137.                         Text<#text>Sid
  138.                 Element<Type>
  139.                         Text<#text>snake
  140.         Element<Pet>
  141.                 Element<ID>
  142.                         Text<#text>4
  143.                 Element<OwnerID>
  144.                         Text<#text>3
  145.                 Element<Name>
  146.                         Text<#text>Tickles
  147.                 Element<Type>
  148.                         Text<#text>spider
  149.         Element<Pet>
  150.                 Element<ID>
  151.                         Text<#text>5
  152.                 Element<OwnerID>
  153.                         Text<#text>1
  154.                 Element<Name>
  155.                         Text<#text>Tweetie
  156.                 Element<Type>
  157.                         Text<#text>canary
  158. </xmp></div>
  159.  
  160. <H4>Summary</H4>
  161. <OL>
  162. <LI>The XmlDataDocument can be constructed from a DataSet to provide an XML API onto the relational data.
  163. <LI>Data that is entered via the relation methods on the DataSet can be accessed via the XML methods on the XmlDataDocument.
  164. <LI>XML Data can be saved from both the DataSet with the WriteXmlData method and the XmlDataDocument with the Save method. The former saves a 
  165. normalized view of the relationally mapped data, whilst the latter saves the XML with full fidelity. If the data has only been entered via the
  166. DataSet, then these methods are equivalent.
  167. </LI></OL>
  168.  
  169. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.