Skip to content

Instantly share code, notes, and snippets.

@johnnyreilly
Created November 2, 2012 11:21
Show Gist options
  • Save johnnyreilly/4000326 to your computer and use it in GitHub Desktop.
Save johnnyreilly/4000326 to your computer and use it in GitHub Desktop.
Demo of how to easily serialize and deserialize objects to and from XML
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.239
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
namespace MyNameSpace {
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class contact {
private string firstNameField;
private string lastNameField;
private sbyte heightInInchesField;
private string typeField;
/// <remarks/>
public string firstName {
get {
return this.firstNameField;
}
set {
this.firstNameField = value;
}
}
/// <remarks/>
public string lastName {
get {
return this.lastNameField;
}
set {
this.lastNameField = value;
}
}
/// <remarks/>
public sbyte heightInInches {
get {
return this.heightInInchesField;
}
set {
this.heightInInchesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<contact type="personal">
<firstName>John</firstName>
<lastName>Reilly</lastName>
<heightInInches>76</heightInInches>
</contact>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="firstName"/>
<xs:element type="xs:string" name="lastName"/>
<xs:element type="xs:byte" name="heightInInches"/>
</xs:sequence>
<xs:attribute type="xs:string" name="type"/>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
<xs:element name="lastName" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
<xs:element name="heightInInches" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
</xs:sequence>
<xs:attribute name="type" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="contact" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace My.Helpers
{
public static class XmlConverter<T>
{
private static XmlSerializer _serializer = null;
#region Static Constructor
/// <summary>
/// Static constructor that initialises the serializer for this type
/// </summary>
static XmlConverter()
{
_serializer = new XmlSerializer(typeof(T));
}
#endregion
#region Public
/// <summary>
/// Deserialize the supplied XML into an object
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static T ToObject(string xml)
{
return (T)_serializer.Deserialize(new StringReader(xml));
}
/// <summary>
/// Serialize the supplied object into XML
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToXML(T obj)
{
using (var memoryStream = new MemoryStream())
{
_serializer.Serialize(memoryStream, obj);
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
#endregion
}
}
using MyNameSpace;
//Make a new contact
contact myContact = new contact();
//Serialize the contact to XML
string myContactXML = XmlConverter<contact>.ToXML(myContact);
//Deserialize the XML back into an object
contact myContactAgain = XmlConverter<contact>.ToObject(myContactXML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment