加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

model转XML

发布时间:2020-12-16 23:40:39 所属栏目:百科 来源:网络整理
导读: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

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("rn","");
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,"");

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?

?

?

??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读