home *** CD-ROM | disk | FTP | other *** search
Wrap
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%> <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" --> <h4>How Do I...Save a DataSet as XML?</h4> <div class="indent" style="width:660"> 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 relationally mapped data and XML data. <br clear="left"><br> 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. </div> <h4>Saving a DataSet as XML</h4> <Acme:SourceRef ViewSource="/quickstart/howto/samples/Xml/SaveDataSetXMLData/SaveDataSetXMLData.src" RunSample="/quickstart/howto/samples/Xml/SaveDataSetXMLData/SaveDataSetXMLData.aspx" Icon = "/quickstart/images/genicon.gif" Caption="SaveDataSetXMLData.aspx" runat="server" /> <br clear="left"><br> <div class="indent" style="width:660"> 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 against the internally generated XSD schema. First an XmlDataDocument is created for the DataSet. </div> <div class="code"><xmp> // Load the DataSet with relation data DataSet dataset = new DataSet(); LoadDataSet(dataset); // Create an XmlDataDocument for the DataSet XmlDataDocument datadoc = new XmlDataDocument(dataset); </xmp></div> <div class="indent" style="width:660"> To save the XML to a file the XmlDataDocument <b>Save</b> method is called with a StreamWriter representing the file. </div> <div class="code"><xmp> // Write data as XML internal void WriteXMLData(XmlDataDocument datadoc) { StreamWriter writer = null; try { Console.WriteLine("Writing the XML data to {0} ...", m_NewDocument); writer = new StreamWriter(m_NewDocument); datadoc.Save(writer); } catch (Exception e) { Console.WriteLine ("Exception: {0}", e.ToString()); } finally { if (writer != null) writer.Close(); } } </xmp></div> <div class="indent" style="width:660"> The XML is written to the file <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/SaveDataSetXMLData/&file=PersonPet.xml">PersonPet.xml</a>. The XML in the XmlDataDocument can now be read with an XmlReader and displayed, showing the XML for the relational data. See <A target=content href="StreamAnXmlDocument.aspx">How do I...Read XML from an XmlDataDocument?</A> The output from this is shown below. </div> <div class="code"><xmp> Writing the XML data to PersonPet.xml ... Element<PersonPet> Element<Person> Element<ID> Text<#text>0 Element<Name> Text<#text>Mark Element<Age> Text<#text>18 Element<Person> Element<ID> Text<#text>1 Element<Name> Text<#text>William Element<Age> Text<#text>12 Element<Person> Element<ID> Text<#text>2 Element<Name> Text<#text>James Element<Age> Text<#text>7 Element<Person> Element<ID> Text<#text>3 Element<Name> Text<#text>Levi Element<Age> Text<#text>4 Element<Pet> Element<ID> Text<#text>0 Element<OwnerID> Text<#text>0 Element<Name> Text<#text>Frank Element<Type> Text<#text>cat Element<Pet> Element<ID> Text<#text>1 Element<OwnerID> Text<#text>1 Element<Name> Text<#text>Rex Element<Type> Text<#text>dog Element<Pet> Element<ID> Text<#text>2 Element<OwnerID> Text<#text>2 Element<Name> Text<#text>Cottontail Element<Type> Text<#text>rabbit Element<Pet> Element<ID> Text<#text>3 Element<OwnerID> Text<#text>3 Element<Name> Text<#text>Sid Element<Type> Text<#text>snake Element<Pet> Element<ID> Text<#text>4 Element<OwnerID> Text<#text>3 Element<Name> Text<#text>Tickles Element<Type> Text<#text>spider Element<Pet> Element<ID> Text<#text>5 Element<OwnerID> Text<#text>1 Element<Name> Text<#text>Tweetie Element<Type> Text<#text>canary </xmp></div> <H4>Summary</H4> <OL> <LI>The XmlDataDocument can be constructed from a DataSet to provide an XML API onto the relational data. <LI>Data that is entered via the relation methods on the DataSet can be accessed via the XML methods on the XmlDataDocument. <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 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 DataSet, then these methods are equivalent. </LI></OL> <!-- #include virtual="/quickstart/howto/include/footer.inc" -->