其他分享
首页 > 其他分享> > 记:.net中xml各种实现

记:.net中xml各种实现

作者:互联网

public sealed class StringWriterWithEncoding : StringWriter
{
private readonly Encoding encoding;

public StringWriterWithEncoding() : this(Encoding.UTF8) { }

public StringWriterWithEncoding(Encoding encoding)
{
this.encoding = encoding;
}

public override Encoding Encoding
{
get { return encoding; }
}
}

public static class XmlHelper
{
/// <summary>
/// Serialize class instance to XML file
/// </summary>
/// <typeparam name="T">type</typeparam>
/// <param name="XMLFileToCreate">XMLFileToCreate</param>
/// <param name="instance">class instance</param>
/// <param name="namespaces">class instance</param>
public static void Serialize<T>(string XMLFileToCreate, T instance, XmlSerializerNamespaces namespaces)
{
if (instance == null) return;

XmlSerializer xs = new XmlSerializer(typeof(T));
using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
{
xs.Serialize(sw, instance, namespaces);
}
}

/// <summary>
/// Serialize class instance to XML string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance"></param>
/// <param name="namespaces"></param>
/// <returns></returns>
public static string Serialize<T>(T instance, XmlSerializerNamespaces namespaces)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

using (StringWriter stringWriter = new StringWriter(stringBuilder))
{
xmlSerializer.Serialize(stringWriter, instance, namespaces);

return stringWriter.ToString();
}
}

/// <summary>
/// Serialize class instance to XML string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance"></param>
/// <param name="namespaces"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string SerializeEncoding<T>(T instance, XmlSerializerNamespaces namespaces, Encoding encoding = null)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

if (encoding == null)
encoding = Encoding.UTF8;

using (StringWriterWithEncoding stringWriter = new StringWriterWithEncoding(encoding))
{
xmlSerializer.Serialize(stringWriter, instance, namespaces);

return stringWriter.ToString();
}
}

/// <summary>
/// Serialize class instance to XML string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="instance"></param>
/// <returns></returns>
public static string Serialize<T>(T instance)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

using (StringWriter stringWriter = new StringWriter(stringBuilder))
{
xmlSerializer.Serialize(stringWriter, instance);

return stringWriter.ToString();
}
}

public static void Serialize<T>(string XMLFileToCreate, T instance)
{
if (instance == null) return;

XmlSerializer xs = new XmlSerializer(typeof(T));

using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
{
xs.Serialize(sw, instance);
}
}

public static void SerializeToGZip<T>(string zippath, string xmlname, T instance)
{
if (instance == null) return;

XmlSerializer xs = new XmlSerializer(typeof(T));

using (Stream sw = new MemoryStream())
{
xs.Serialize(sw, instance);

GZipHelper.StreamToGZip(sw, zippath, xmlname);
}
}

#region 自动读取XML数据到实体类

/// <summary>
/// 自动读取XML文件路径数据到实体类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="XmlFilePath"></param>
/// <returns></returns>
public static T DeserializePath<T>(string XmlFilePath)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (FileStream fs = new FileStream(XmlFilePath, FileMode.Open))
{
var s = fs.Name;
return (T)xs.Deserialize(fs);
}
}

/// <summary>
/// 自动读取XML字符数据到实体类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xmlText"></param>
/// <returns></returns>
public static T DeserializeXmlText<T>(string xmlText)
{
using (StringReader sr = new StringReader(xmlText))
{
XmlSerializer xmldes = new XmlSerializer(typeof(T));
return (T)xmldes.Deserialize(sr);
}
}

#endregion 自动读取XML数据到实体类
}

标签:xml,string,实现,Serialize,public,instance,new,net,XmlSerializer
来源: https://www.cnblogs.com/yangruibokeyuan/p/16374836.html