Object Serialize TO Xml

Two Functions to Serialize and detribalize any objects to and from xml

 

 

private string Serialize(object obj) 

{ 

     if (obj == null) { return null; }

     XmlSerializer s = new XmlSerializer(obj.GetType()); 

     TextWriter w = new StringWriter(); 

     s.Serialize(w, obj); 

     w.Close();

     return w.ToString(); 

}

 

 

public static object Deserialize(Type type, string xml) 

{ 

 

 if (string.IsNullOrEmpty(xml)) 

 { 

     return Activator.CreateInstance(type); 

 } 

 

    XmlSerializer s = new XmlSerializer(type); 

    TextReader tr = new StringReader(xml); 

    return s.Deserialize(tr); 

}

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
TAGS:  , , ,

 

View Abdel-Rahman Awad's profile on LinkedIn

Archives

 

Rss Feed Tweeter button Facebook button Linkedin button Delicious button Digg button