<%@Page Language="vb" Debug="true" Trace="false"%> <%@Import Namespace="System"%> <%@Import Namespace="System.XML"%> <%@Import Namespace="System.XML.Serialization"%> <%@Import Namespace="System.IO"%> <script language="vb" runat="server"> '*********************** PERSON CLASS *********************** <XMLRoot(ElementName:="Class_Person")> _ public class Person '*** Data Members *** private m_sName as string private m_iAge as integer '*** /Data Members *** '*** Properties *** <XMLElement(ElementName:="Property_Name")> _ public property Name() as string get return m_sName end get set(byval sNewName as string) m_sName = sNewName end set end property <XMLElement(ElementName:="Property_Age")> _ public property Age() as integer get return m_iAge end get set(byval iNewAge as integer) m_iAge = iNewAge end set end property '*** /Properties *** '*** Methods *** public function Hello() as string dim s as string s = "Hi! My name is " & Name & " and I am " & Age & " years old." return s end function public function Goodbye() as string return "So long!" end function '*** /Methods *** end class '*********************** /PERSON CLASS *********************** public function FormatXMLToHTML(byval sXML as string) as string sXML = sXML.Replace("<", "&lt;").Replace(">", "&gt;").Replace(vbNewLine, "<br />") return sXML end function </script> <html> <head> <title>XML Serialization Test</title> </head> <body> <% '******************************************************************* 'SERIALIZE TO A FILE dim oXS as XMLSerializer = new XMLSerializer(GetType(Person)) dim oLucky as new Person() dim oStmW as StreamWriter 'Set properties oLucky.Name = "Lucky Day" oLucky.Age = 52 'Display property values Response.Write("<b>1. Before Serialization</b>") Response.Write("<br />") Response.Write("Hello() = " & oLucky.Hello() & "<br />") Response.Write("Goodbye() = " & oLucky.Goodbye() & "<br />") 'Serialize object to XML and write it to XML file oStmW = new StreamWriter(Server.MapPath("lucky.xml")) oXS.Serialize(oStmW, oLucky) oStmW.Close() Response.Write("<hr>") 'Kill Lucky oLucky = nothing '******************************************************************* 'DESERIALIZE FROM A FILE dim oNed as Person dim oStmR as StreamReader 'Pull in contents of an object serialized into an XML file 'and deserialize it into an object oStmR = new StreamReader(Server.MapPath("ned.xml")) oNed = oXS.Deserialize(oStmR) oStmR.Close() 'Display property values Response.Write("<br />") Response.Write("<b>2. Deserialized Object</b>") Response.Write("<br />") Response.Write("Hello() = " & oNed.Hello() & "<br />") Response.Write("Goodbye() = " & oNed.Goodbye() & "<br />") Response.Write("<hr>") 'Kill Ned oNed = nothing '******************************************************************* 'SERIALIZE TO AN XML STRING dim oDusty as new Person() dim oStrW as new StringWriter() dim sXML as string 'Set properties oDusty.Name = "Dusty Bottoms" oDusty.Age = 51 'Serialize object into an XML string oXS.Serialize(oStrW, oDusty) sXML = oStrW.ToString() oStrW.Close() 'Display XML Response.Write("<br />") Response.Write("<b>3. XML resulting from Serialization</b>") Response.Write("<br />") Response.Write(FormatXMLToHTML(sXML) & "<br />") Response.Write("<hr>") 'Kill Dusty oDusty = nothing '******************************************************************* %> </body> </html>