We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 05/16/07


Tagged

xml serialization StringWriter XmlTextWriter XmlTextReader StringReader


Versions (?)


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.