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


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  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

You need to login to post a comment.