Posts
33
Comments
186
Trackbacks
0
XML Serialization - example

Serialization and De-serialization is very important concept in .Net world. I won't go in-depth of what each of them means.

In short, Serialization means converting your .Net object into some preservable format like xml string.

De-serialization is the exact opposite of this and it means converting your string into C# object.

In this article, I will show you a very simple working example of Serialization.

Suppose you want to Serializa an object obj of type obj_Type

Step 1:  Include System.Xml.Serialization namespace in your code

Step 2: Put the following lines of code

XmlSerializer xml = new XmlSerializer(typeof(obj_Type));

System.IO.TextWriter txt = new System.IO.StreamWriter(@"C:\test.xml");

xml.Serialize(txt, obj);

txt.close();

Following execution of this code, your object would be serialized into an xml file.

Hope this helps !!

posted on Friday, October 1, 2010 10:19 AM Print
Comments
Gravatar
# re: XML Serialization - example
jhjh
10/14/2011 10:57 AM
[XmlRoot("dictionary")]

public class SerializableDictionary<TKey, TValue>

: Dictionary<TKey, TValue>, IXmlSerializable

{

#region IXmlSerializable Members

public System.Xml.Schema.XmlSchema GetSchema()

{

return null;

}



public void ReadXml(System.Xml.XmlReader reader)

{

XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));



bool wasEmpty = reader.IsEmptyElement;

reader.Read();



if (wasEmpty)

return;



while (reader.NodeType != System.Xml.XmlNodeType.EndElement)

{

reader.ReadStartElement("item");



reader.ReadStartElement("key");

TKey key = (TKey)keySerializer.Deserialize(reader);

reader.ReadEndElement();



reader.ReadStartElement("value");

TValue value = (TValue)valueSerializer.Deserialize(reader);

reader.ReadEndElement();



this.Add(key, value);



reader.ReadEndElement();

reader.MoveToContent();

}

reader.ReadEndElement();

}



public void WriteXml(System.Xml.XmlWriter writer)

{

XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));



foreach (TKey key in this.Keys)

{

writer.WriteStartElement("item");



writer.WriteStartElement("key");

keySerializer.Serialize(writer, key);

writer.WriteEndElement();



writer.WriteStartElement("value");

TValue value = this[key];

valueSerializer.Serialize(writer, value);

writer.WriteEndElement();



writer.WriteEndElement();

}

}

#endregion

}
Gravatar
# iphone customer care phone number
apple customer service
4/7/2017 6:49 AM
Apple iPhone predominantly has played the change agent in the mobile phone market. It has provided a very dynamic experience to the customers.
Gravatar
# re: XML Serialization - example
apple customer service
4/7/2017 6:50 AM
Apple iPhone predominantly has played the change agent in the mobile phone market. It has provided a very dynamic experience to the customers.
iphone customer care phone number<br />
Gravatar
# <a href="http://greatest-trends.com/laptops-for-programming/"><b>laptops for programming and coding</b></a>
best laptops for programming and
5/9/2017 7:51 AM
programming laptops for college students

Post Comment

Title *
Name *
Email
Comment *  
Verification