using EasyDevCore.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace EasyDevCore.Database.EntityTable
{
///
///
///
public static class SerializerExtensions
{
///
/// Converts to xmlraw.
///
/// The table.
/// Name of the row.
/// Name of the document.
///
public static string ToRawXml(this DataTable table, string rowName = "Row", string documentName = null)
{
if (string.IsNullOrWhiteSpace(documentName)) documentName = "DATA";
using (StringWriter sw = new StringWriter())
{
sw.WriteLine(string.Format(@"<{0} xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">", documentName));
foreach (DataRow row in table.Rows)
{
sw.WriteLine(string.Format("<{0}>", rowName));
foreach (DataColumn col in table.Columns)
{
object value = row.GetValue(col.Ordinal);
if (value == null)
{
sw.WriteLine(string.Format("<{0} xsi:nil=\"true\"/>", XmlConvert.EncodeName(col.ColumnName)));
}
else
{
sw.WriteLine(string.Format("<{0}>{1}{0}>", XmlConvert.EncodeName(col.ColumnName), System.Security.SecurityElement.Escape(value.StringFrom())));
}
}
sw.WriteLine(string.Format("{0}>", rowName));
}
sw.WriteLine(string.Format(@"{0}>", documentName));
return sw.ToString();
}
}
///
/// Converts to xml.
///
/// The source.
/// Name of the element.
/// if set to true [for synch values].
/// The column names.
/// The except column names.
/// The key fields.
/// if set to true [omit XML declaration].
/// The proxy shema.
/// if set to true [optimum size].
///
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)
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = omitXmlDeclaration;
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
if (elementName == null) elementName = typeof(DataTableSerializer).Name;
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = elementName;
XmlSerializer serializer = new XmlSerializer(typeof(DataTableSerializer), xRoot);
DataTableSerializer ps = new DataTableSerializer(source, forSynchValues, columnNames, exceptColumnNames, keyFields, proxyShema, optimumSize);
serializer.Serialize(writer, ps);
}
return sb.ToString();
}
///
/// Gets the table from XML.
///
/// The XML.
/// Name of the root.
///
public static DataTable GetTableFromXML(this string xml, string rootName = null)
{
using (StringReader sr = new StringReader(xml))
{
if (rootName == null)
rootName = typeof(DataTableSerializer).Name;
XmlRootAttribute xRoot = new XmlRootAttribute(rootName);
xRoot.ElementName = rootName;
XmlSerializer serializer = new XmlSerializer(typeof(DataTableSerializer), xRoot);
DataTableSerializer px = (DataTableSerializer)serializer.Deserialize(sr);
return px.Table;
}
}
}
}