XML Serialization Demo

This demo illustrates how to use .NET features to serialize objects into XML files. The output can be seen below.


1. Before Serialization
Hello() = Hi! My name is Lucky Day and I am 52 years old.
Goodbye() = So long!



2. Deserialized Object
Hello() = Hi! My name is Ned Nederlander and I am 47 years old.
Goodbye() = So long!


3. XML resulting from Serialization
<?xml version="1.0" encoding="utf-16"?>
<Class_Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Property_Name>Dusty Bottoms</Property_Name>
<Property_Age>51</Property_Age>
</Class_Person>


Source Code
<%@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("<", "<").Replace(">", ">").Replace(vbNewLine, "<br />")
		return sXML
	end function
</script>


<html>
<head>
	<title>XML Serialization Demo</title>
</head>
<body>
<h1>XML Serialization Demo</h1>
This demo illustrates how to use .NET features to serialize objects into XML files.
The output can be seen below.
<p><Hr><p>
<%
'*******************************************************************

'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

'*******************************************************************
%>


[Return to the article]