How to Create XML Serialization in .NET

See XML: Tips and Tricks for similar articles.

The steps below describe how to implement XML serialization in .NET.

XML Serilization is the process of extracting the PUBLIC properties and fields of an object and creating an XML stream which can then be persisted locally or ransmitted across a network connection. This stream can the be used to create a copy of the object by instantiating the object and applying all the public property and field values from the stream (aka Deserilization). It is important to note that only the public properties and fields are serilized. Private properties and fields are not serilized and thus cannot be applied to the copy of the object. If it is necessary to persist private field or property values, you should explore either implementing the ISerilizable interface or using Binary Serilization as an alternative.

  1. To begin, we need a class that can be serialized: C#
    
    	public class Foo
    		{
    			public string bar1; //This is a public field and the value will be serilized
    			
    			private string bar2="WXYZ"; //This is a Private string and will NOT be serilized
    	
    			public string bar3 //This is a public property and the value will be serilized
    			{ get; set; }
    	
    		}
    		
    VB.Net
    
    	Public Class Foo
    	
    		Public bar1 As String  'This is a public field and the value will be serilized
    	
    		Private bar2 As String = "wxyz"  'This is a Private string and will NOT be serilized
    	
    		Public Property bar3 As String  'This is a public property and the value will be serilized
    	
    	End Class
    	
  2. Next, we bring in the appropriate namespace C#
    
    	using System.Xml.Serialization;
    	
    VB.Net
    
    	Imports System.Xml.Serialization
    	
  3. Finally, we create an instance of the class to be serilized, set the properties on the object, instantiate the Serilizer class and call the serilize method passing a stream to write to and the object to be serilized. C#
    
    	//Create an instance of the object to be serilized
    	Foo F = new Foo();
    	
    	//Set the property Values
    	F.bar1 = "abc123";
    	F.bar3 = "123ABC";
    	
    	//Instantiate the Serilizer
    	XmlSerializer Ser = new XmlSerializer(typeof(ClassLibrary2.Foo));
    	
    	//The Serilizer requires a stream to write to, so we create a StreamWriter
    	//that writes to a file on the desktop
    	TextWriter Wri = new StreamWriter(@"C:\Users\Michael\Desktop\Output.txt");
    	
    	//Call the Serilize method on the serilizer object
    	//Passing the stream and the object to be serilized
    	Ser.Serialize(Wri, F);
    	
    VB.Net
    
    	'Create an instance of the object to be serilized
    	Dim F As New Foo()
    	
    	'Set the property Values
    	F.bar1 = "abc123"
    	F.bar3 = "123ABC"
    	
    	'Instantiate the Serilizer
    	Dim Ser As New XmlSerializer(F.GetType)
    	
    	'The Serilizer requires a stream to write to, so we create a StreamWriter
    	'that writes to a file on the desktop
    	Dim WRI As New StreamWriter(@"C:\Users\Michael\Desktop\Output.txt")
    	
    	'Call the Serilize method on the serilizer object
    	'Passing the stream and the object to be serilized
    	Ser.Serialize(WRI, F)
    	
  4. Now, if we inspect the output file we find the following XML:
    <?xml version="1.0" encoding="utf-8"?>
    	<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    		<bar1>abc123</bar1>
    		<bar3>123ABC</bar3>
    	</Foo>
  5. The object has been serilized (i.e. the public properties have been persisted in a stream of XML). Note that the private field "bar2" was NOT persisted as part of the stream because it is private. Only public Fields and Properties are persisted.

Related Articles

  1. How to Try XMLSpy for Free
  2. How to Send and Receive XML Data to and from the Server
  3. How to Dynamically Populate an HTML Table with XML Data
  4. How to Read XML Files with the Google Maps API
  5. How to Create XML Serialization in .NET (this article)