<%@ Page Language="C#" MasterPageFile="~/howto/howto.master" %>
<%@ Register TagPrefix=Acme Namespace=Acme %>
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="~/util/SrcRef.ascx"%>

<asp:Content ContentPlaceHolderID="MainBody" Runat=Server>
<h4>How do I read/write objects into xml?</h4>

<p>This topic describes how to automatically map a particular XML stream into a set of objects designed to hold the XML using the XML Serialization classes.<p>

<ol>
<li>First, you will need classes designed to work with the XML Serialization classes.  If you have an XSD schema that describes the format of the XML file you want to load or save, you are almost there.  <a href="XsdToCls.aspx">Use the XSD.exe tool</a> to create these classes automatically. You can also build the classes manually. You can find more information on how to do this in the <a href="XsdFromCls.aspx">"How do I Create an XSD schema from a class?"</a> topic. <p>
This example will be using the <b>PurchaseOrder</b> classes that were generated to read XML in the format defined by the purchase order schema.  Both the classes and the schema are shown below.<p>

<table border=0>
<tr><td>
<Acme:LangSwitch runat="server">
  <CsTemplate>
        <Acme:SourceRef
        ViewSource="~/howto/samples/Xmlserialization/ReadWriteXML.src"
        Icon="../../../images/genicon.gif"
        Caption="C# PurchaseOrder.cs"
	Samplepath="howto\samples\Xmlserialization\"
        runat="server" />
  </CsTemplate>
  <VbTemplate>
        <Acme:SourceRef
        ViewSource="~/howto/samples/Xmlserialization/ReadWriteXML.src"
        Icon="../../../images/genicon.gif"
        Caption="VB PurchaseOrder.vb"
	Samplepath="howto\samples\Xmlserialization\"
        runat="server" />
  </VbTemplate>
  <CpTemplate>
        <Acme:SourceRef
        ViewSource="~/howto/samples/Xmlserialization/ReadWriteXML.src"
        Icon="../../../images/genicon.gif"
        Caption="C++ PurchaseOrder.cpp"
	Samplepath="howto\samples\Xmlserialization\"
        runat="server" />
  </CpTemplate>
 <VjsTemplate>
       <Acme:SourceRef
        RunSample=""
        ViewSource=""
        Icon = ""
        Caption=""
        runat="server" />
  </VjsTemplate>
</Acme:LangSwitch>

</td>
<td>
        <Acme:SourceRef
        ViewSource="~/howto/samples/Xmlserialization/PurchaseOrder.Xsd"
        Icon="../../../images/genicon.gif"
        Caption="PurchaseOrder.XSD"
	Samplepath="howto\samples\Xmlserialization\"
        runat="server" />
</td></tr>
</table>
<p>

<li>Create an instance of the <b>XmlSerializer</b>, passing the type of the object you would like to deserialize.  Here we will use the <b>PurchaseOrder</b> type that was defined earlier.<p>
<div class="code"><xmp>
XmlSerializer serializer = new XmlSerializer(typeof(PurchaseOrder));
</xmp></div>
<li>To read the file, call the Deserialize method passing in a Stream, TextReader, or XmlReader.  A purchase order will be returned.
<p>
<table cellpadding=0 cellspacing=0 width="95%">
<tr>
<td>

<Acme:TabControl runat="server">
<Tab Name="C#">
TextReader reader = new StreamReader("po.xml");
PurchaseOrder po = (PurchaseOrder)serializer.Deserialize(reader);
reader.Close();
</Tab>
<Tab Name="VB">
Dim reader As TextReader = New StreamReader("po.xml")
Dim po As PurchaseOrder = CType(serializer.Deserialize(reader), PurchaseOrder)
reader.Close()
</Tab>
<Tab Name="C++">
TextReader ^ reader = new StreamReader("..\\PurchaseOrder.xml");
PurchaseOrder ^ po = __try_cast<PurchaseOrder^>(serializer->Deserialize(reader));
reader->Close();
</Tab>
</Acme:TabControl>

</td>
</tr>
</table>
<p>


<li>To write the file, call the Serialize method passing in a <b>Stream</b>, <b>TextReader</b>, or <b>XmlReader</b> as well as an instance of the purchase order.


<p>
<table cellpadding=0 cellspacing=0 width="95%">
<tr>
<td>

<Acme:TabControl runat="server">
<Tab Name="C#">
TextWriter writer = new StreamWriter("po2.xml");
serializer.Serialize(writer, po);
writer.Close();
</Tab>
<Tab Name="VB">
Dim writer As TextWriter = New StreamWriter("po2.xml")
serializer.Serialize(writer, po)
writer.Close()
</Tab>
<Tab Name="C++">
TextWriter ^ writer = new StreamWriter("PurchaseOrder2.xml");
serializer->Serialize(writer, po);
writer->Close();
</Tab>
</Acme:TabControl>

</td>
</tr>
</table>


</ol>

The following example puts these ideas together by reading the purchase order XML file shown below, and then writing it back out to another file.<p>

<table border=0>
<tr><td>

<Acme:LangSwitch runat="server">
  <CsTemplate>
        <Acme:SourceRef
        RunSample =""
        ViewSource="~/howto/samples/Xmlserialization/ReadWriteXml.src"
        Icon="../../../images/genicon.gif"
        Caption="C# ReadWriteXML"
        runat="server" />
  </CsTemplate>
  <VbTemplate>
        <Acme:SourceRef
        RunSample =""
        ViewSource="~/howto/samples/Xmlserialization/ReadWriteXml.src"
        Icon="../../../images/genicon.gif"
        Caption="VB ReadWriteXML"
        runat="server" />
  </VbTemplate>
  <CpTemplate>
        <Acme:SourceRef
        RunSample =""
        ViewSource="~/howto/samples/Xmlserialization/ReadWriteXml.src"
        Icon = "/images/genicon.gif"
        Caption="C++ ReadWriteXML"
        runat="server" />
  </CpTemplate>
 <VjsTemplate>
       <Acme:SourceRef
        RunSample=""
        ViewSource=""
        Icon = ""
        Caption=""
        runat="server" />
  </VjsTemplate>
</Acme:LangSwitch>

</td><td>
        <Acme:SourceRef
        ViewSource="~/howto/samples/Xmlserialization/PurchaseOrderXml.src"
        Icon="../../../images/genicon.gif"
        Caption="PurchaseOrder.xml"
        runat="server" />

</td></tr>
</table>


</asp:Content>

