Posted By

rengber on 05/16/07


Tagged

xml serialization StringWriter XmlTextWriter XmlTextReader StringReader


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
ddevening


Serializing and Deserializing a Class Created with XSD.Exe Using XML Strings


 / Published in: C#
 

  1. public static string SerializeToXmlString(object targetInstance)
  2. {
  3. string retVal = string.Empty;
  4. TextWriter writer = new StringWriter();
  5. XmlSerializer serializer = new XmlSerializer(targetInstance.GetType());
  6. serializer.Serialize(writer, targetInstance);
  7. retVal = writer.ToString();
  8. return retVal;
  9. }
  10. public static object DeserializeFromXmlString(string objectXml, Type targetType)
  11. {
  12. object retVal = null;
  13. XmlSerializer serializer = new XmlSerializer(targetType);
  14. StringReader stringReader = new StringReader(objectXml);
  15. XmlTextReader xmlReader = new XmlTextReader(StringReader);
  16. retVal = serializer.Deserialize(xmlReader);
  17. return retVal;
  18. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: class3 on January 2, 2008

ddowm

You need to login to post a comment.