A serializable C # object, how to turn it into an XML-formatted file or string "Go"

Source: Internet
Author: User

Original: http://blog.csdn.net/otong/article/details/7894059

Serialize or deserialize into a string:

Method One:

Serialization:

[CSharp]View PlainCopy
  1. Public static string xmlserialize<t> (T entity)
  2. {
  3. StringBuilder buffer = new StringBuilder ();
  4. XmlSerializer serializer = new XmlSerializer (typeof (T));
  5. using (TextWriter writer = new StringWriter (buffer))
  6. {
  7. Serializer. Serialize (writer, entity);
  8. }
  9. return buffer.  ToString ();
  10. }


Deserialization:

[CSharp]View PlainCopy
  1. Public static T dexmlserialize<t> (string xmlstring)
  2. {
  3. T cloneobject = default (t);
  4. StringBuilder buffer = new StringBuilder ();
  5. Buffer. Append (xmlstring);
  6. XmlSerializer serializer = new XmlSerializer (typeof (T));
  7. using (TextReader reader = new StringReader (buffer. ToString ()))
  8. {
  9. Object obj = serializer. Deserialize (reader);
  10. Cloneobject = (T) obj;
  11. }
  12. return cloneobject;
  13. }

Method Two:

[CSharp]View PlainCopy
  1. <summary>
  2. /// object serialized into XML String
  3. // </summary>
  4. public static string xmlserialize<t> (T obj)
  5. {
  6. string xmlstring = string.  Empty;
  7. XmlSerializer XmlSerializer = new XmlSerializer (typeof (T));
  8. using (MemoryStream ms = New MemoryStream ())
  9. {
  10. Xmlserializer.serialize (MS, obj);
  11. XMLString = Encoding.UTF8.GetString (ms. ToArray ());
  12. }
  13. return xmlstring;
  14. }
  15. // <summary>
  16. /// XML String deserialized into an object
  17. // </summary>
  18. public static T xmldeserialize<t> (string xmlstring)
  19. {
  20. T t = default (t);
  21. XmlSerializer XmlSerializer = new XmlSerializer (typeof (T));
  22. using (Stream xmlstream = new MemoryStream (Encoding.UTF8.GetBytes (xmlstring)))
  23. {
  24. using (XmlReader XmlReader = Xmlreader.create (xmlstream))
  25. {
  26. Object obj = xmlserializer.deserialize (xmlReader);
  27. t = (t) obj;
  28. }
  29. }
  30. return t;
  31. }

If you want to serialize to a file, you can use FileStream.

Such as:

String strfile = @ "C:/book.xml";
using (FileStream fs = new FileStream (strfile, FileMode.Create))
{
XmlSerializer formatter = new XmlSerializer (typeof (T));
Formatter. Serialize (FS, entity);
}

Attached: In fact, can also be directly used Sringwriter

StringBuilder sb = new StringBuilder ();
System.IO.StringWriter writer = new System.IO.StringWriter (SB);

XmlSerializer formatter = new XmlSerializer (typeof (T));
Formatter. Serialize (writer,entity);

A serializable C # object, how to turn it into an XML-formatted file or string "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.