You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
809 B

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml.Serialization;
  7. namespace Shentun.Peis.PlugIns.Gem
  8. {
  9. public class XmlHelper
  10. {
  11. public static string SerializeToXml<T>(T obj)
  12. {
  13. XmlSerializer serializer = new XmlSerializer(typeof(T));
  14. using (StringWriter textWriter = new StringWriter())
  15. {
  16. serializer.Serialize(textWriter, obj);
  17. return textWriter.ToString();
  18. }
  19. }
  20. public static T DeserializeXml<T>(string xml)
  21. {
  22. var serializer = new XmlSerializer(typeof(T));
  23. using var reader = new StringReader(xml);
  24. return (T)serializer.Deserialize(reader);
  25. }
  26. }
  27. }