model转XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;
using System.Text.RegularExpressions;

namespace Common.Tools
{
/// <summary>
/// 序列化与反序列化帮助类
/// </summary>
public class ModelSerializer
{
/// <summary>
/// 序列化输出到文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="FilePath">文件保存路径 例:"I:\\ab.xml"</param>
/// <returns></returns>
public static bool SerializerToFile<T>(T model, string FilePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
serializer.Serialize(File.Create(FilePath), model);
return true;
}
catch (Exception)
{
return false;
}
}

/// <summary>
/// 序列化并返回字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="xmlPrefix">xml前缀</param>
/// <param name="xmlNamespace">xml命名空间</param>
/// <returns></returns>
public static string SerializerToString<T>(T model, string xmlPrefix, string xmlNamespace)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringWriter write = new StringWriter();

try
{
XmlSerializerNamespaces spaces = new XmlSerializerNamespaces();
spaces.Add(xmlPrefix, xmlNamespace);
serializer.Serialize(write, model, spaces);
string Val = write.ToString().Replace("\r\n", "");
Val = Val.Replace("utf-16", "UTF-8");
//屏蔽节点
Val = new Regex("<IgnoreName>").Replace(Val, "");
Val = new Regex("</IgnoreName>").Replace(Val, "");
Val = new Regex("<IgnoreName />").Replace(Val, "");
return Val;
}
catch (Exception)
{
return "";
}
}

/// <summary>
/// 序列化并返回字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="emptyToPair">空串序列化为成对标签</param>
/// <param name="xmlPrefix">xml前缀</param>
/// <param name="xmlNamespace">xml命名空间</param>
/// <returns></returns>
public static string SerializerToString<T>(T model, bool emptyToPair, string xmlPrefix, string xmlNamespace)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringWriter write = new StringWriter();

try
{
XmlSerializerNamespaces spaces = new XmlSerializerNamespaces();
spaces.Add(xmlPrefix, xmlNamespace);
serializer.Serialize(write, model, spaces);
string Val = write.ToString().Replace("\r\n", "");
Val = Val.Replace("utf-16", "UTF-8");
//屏蔽节点
Val = new Regex("<IgnoreName>").Replace(Val, "");
Val = new Regex("</IgnoreName>").Replace(Val, "");
Val = new Regex("<IgnoreName />").Replace(Val, "");

if (emptyToPair)
{
Regex reg = new Regex("<([a-zA-Z]*) />");

var gf = reg.Matches(Val);
foreach (Match item in gf)
{
if (item.Groups.Count > 1)
{
string elementName = item.Groups[1].Value;
string sourceStr = string.Format("<{0} />", elementName);
string targetStr = string.Format("<{0}></{1}>", elementName, elementName);
Val = Val.Replace(sourceStr, targetStr);
}
}
}

return Val;
}
catch (Exception)
{
return "";
}
}


/// <summary>
/// 将指定目录的xml反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="path"></param>
/// <returns></returns>
public static T DeserializerWithFilePath<T>(T model, string path)
{
StreamReader reader = new StreamReader(path);
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
object obj = serializer.Deserialize(reader);
reader.Close();
model = (T)obj;
return model;
}
catch (Exception)
{
return model;
}
}

/// <summary>
/// 将xml字符串反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="xmlString"></param>
/// <returns></returns>
public static T DeserializerWithXmlString<T>(T model, string xmlString)
{
xmlString = xmlString.Replace("\n", "");
xmlString = xmlString.Trim();
xmlString = xmlString.Replace(@"\", "");
StringReader reader = new StringReader(xmlString);
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
object obj = serializer.Deserialize(reader);
reader.Close();
model = (T)obj;
return model;
}
catch (Exception)
{
return model;
}

}
}
}

 

 

 

使用方法Model转XML

调用方法:SerializerToString

命名空间:Common.Tools

示例:

Model

 

 

操作

 

 

结果

 

 

 

其它:

1.自定义命名根节点:使用XmlRoot,例如

       

 

 

生成的xml中根节点就由SampleClass变成SC

 

2.字段转上一节点属性,使用XmlAtrribute

     

 

 

 

3.字段重命名,使用XmlElement 

 

   

 

转载于:https://www.cnblogs.com/nayilvyangguang/p/10078811.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值