XmlBuilder.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Serialization;
  4. using System.Text;
  5. using COSXML.Model.Tag;
  6. using System.Xml;
  7. using System.IO;
  8. using COSXML.Utils;
  9. namespace COSXML.Transfer
  10. {
  11. public sealed class XmlBuilder
  12. {
  13. public static String Serialize(object o)
  14. {
  15. var emptyNs = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
  16. var settings = new XmlWriterSettings();
  17. settings.Indent = true;
  18. settings.OmitXmlDeclaration = true;
  19. XmlSerializer serializer = new XmlSerializer(o.GetType());
  20. using (var stream = new StringWriter())
  21. using (var writer = XmlWriter.Create(stream, settings))
  22. {
  23. serializer.Serialize(writer, o, emptyNs);
  24. return stream.ToString();
  25. }
  26. }
  27. // private static string RemoveXMLHeader(string xmlContent)
  28. // {
  29. // if (xmlContent != null)
  30. // {
  31. // if (xmlContent.StartsWith("<?xml"))
  32. // {
  33. // int end = xmlContent.IndexOf("?>");
  34. // xmlContent = xmlContent.Substring(end + 2);
  35. // }
  36. // }
  37. // return xmlContent;
  38. // }
  39. }
  40. }