SerializerExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using EasyDevCore.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. using System.Xml.Linq;
  11. using System.Xml.Serialization;
  12. namespace EasyDevCore.Database.EntityTable
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. public static class SerializerExtensions
  18. {
  19. /// <summary>
  20. /// Converts to xmlraw.
  21. /// </summary>
  22. /// <param name="table">The table.</param>
  23. /// <param name="rowName">Name of the row.</param>
  24. /// <param name="documentName">Name of the document.</param>
  25. /// <returns></returns>
  26. public static string ToRawXml(this DataTable table, string rowName = "Row", string documentName = null)
  27. {
  28. if (string.IsNullOrWhiteSpace(documentName)) documentName = "DATA";
  29. using (StringWriter sw = new StringWriter())
  30. {
  31. sw.WriteLine(string.Format(@"<{0} xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">", documentName));
  32. foreach (DataRow row in table.Rows)
  33. {
  34. sw.WriteLine(string.Format("<{0}>", rowName));
  35. foreach (DataColumn col in table.Columns)
  36. {
  37. object value = row.GetValue(col.Ordinal);
  38. if (value == null)
  39. {
  40. sw.WriteLine(string.Format("<{0} xsi:nil=\"true\"/>", XmlConvert.EncodeName(col.ColumnName)));
  41. }
  42. else
  43. {
  44. sw.WriteLine(string.Format("<{0}>{1}</{0}>", XmlConvert.EncodeName(col.ColumnName), System.Security.SecurityElement.Escape(value.StringFrom())));
  45. }
  46. }
  47. sw.WriteLine(string.Format("</{0}>", rowName));
  48. }
  49. sw.WriteLine(string.Format(@"</{0}>", documentName));
  50. return sw.ToString();
  51. }
  52. }
  53. /// <summary>
  54. /// Converts to xml.
  55. /// </summary>
  56. /// <param name="source">The source.</param>
  57. /// <param name="elementName">Name of the element.</param>
  58. /// <param name="forSynchValues">if set to <c>true</c> [for synch values].</param>
  59. /// <param name="columnNames">The column names.</param>
  60. /// <param name="exceptColumnNames">The except column names.</param>
  61. /// <param name="keyFields">The key fields.</param>
  62. /// <param name="omitXmlDeclaration">if set to <c>true</c> [omit XML declaration].</param>
  63. /// <param name="proxyShema">The proxy shema.</param>
  64. /// <param name="optimumSize">if set to <c>true</c> [optimum size].</param>
  65. /// <returns></returns>
  66. public static string ToXml(this DataTable source, string elementName = null, bool forSynchValues = false, string columnNames = "", string exceptColumnNames = "", string keyFields = "", bool omitXmlDeclaration = false, DataTableSerializerSchema proxyShema = DataTableSerializerSchema.None, bool optimumSize = true)
  67. {
  68. StringBuilder sb = new StringBuilder();
  69. XmlWriterSettings settings = new XmlWriterSettings();
  70. settings.OmitXmlDeclaration = omitXmlDeclaration;
  71. using (XmlWriter writer = XmlWriter.Create(sb, settings))
  72. {
  73. if (elementName == null) elementName = typeof(DataTableSerializer).Name;
  74. XmlRootAttribute xRoot = new XmlRootAttribute();
  75. xRoot.ElementName = elementName;
  76. XmlSerializer serializer = new XmlSerializer(typeof(DataTableSerializer), xRoot);
  77. DataTableSerializer ps = new DataTableSerializer(source, forSynchValues, columnNames, exceptColumnNames, keyFields, proxyShema, optimumSize);
  78. serializer.Serialize(writer, ps);
  79. }
  80. return sb.ToString();
  81. }
  82. /// <summary>
  83. /// Gets the table from XML.
  84. /// </summary>
  85. /// <param name="xml">The XML.</param>
  86. /// <param name="rootName">Name of the root.</param>
  87. /// <returns></returns>
  88. public static DataTable GetTableFromXML(this string xml, string rootName = null)
  89. {
  90. using (StringReader sr = new StringReader(xml))
  91. {
  92. if (rootName == null)
  93. rootName = typeof(DataTableSerializer).Name;
  94. XmlRootAttribute xRoot = new XmlRootAttribute(rootName);
  95. xRoot.ElementName = rootName;
  96. XmlSerializer serializer = new XmlSerializer(typeof(DataTableSerializer), xRoot);
  97. DataTableSerializer px = (DataTableSerializer)serializer.Deserialize(sr);
  98. return px.Table;
  99. }
  100. }
  101. }
  102. }