2 Star 34 Fork 23

Sunday / 代码片段

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
XmlConvert.cs 6.35 KB
一键复制 编辑 原始数据 按行查看 历史
Sunday 提交于 2021-06-10 14:08 . xml序列化转化类
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.Serialization;
namespace BoYuan.Framework.Uitility
{
/// <summary>
/// xml转换工具类
/// </summary>
public class XmlConvert
{
/// <summary>
/// 将实体序列化xml字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string XmlSerialize<T>(T obj)
{
if (obj == null) return string.Empty;
using (StringWriter sw = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(sw, obj);
sw.Close();
return sw.ToString();
}
/*
//序列化
string strxml = XmlSerialize<Request>(patientIn);
*/
}
/// <summary>
/// 将xml字符串反序列化成实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strXML"></param>
/// <returns></returns>
public static T XmlDeSerializer<T>(string strXML) where T : class
{
try
{
using (StringReader sr = new StringReader(strXML.Trim()))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return serializer.Deserialize(sr) as T;
}
}
catch (Exception ex)
{
return null;
}
/*
//反序列化
Request r = XmlDeSerializer<Request>(strxml);
*/
}
/// <summary>
/// 将xml对象内容字符串转换为DataSet
/// </summary>
/// <param name="xmlData"></param>
/// <returns></returns>
public static DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
}
/// <summary>
/// 将DataSet转换为xml对象字符串
/// </summary>
/// <param name="xmlDS"></param>
/// <returns></returns>
public static string ConvertDataSetToXML(DataSet xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
}
/// <summary>
/// 将xml文件转换为DataSet
/// </summary>
/// <param name="xmlPath">xml文件路径</param>
/// <returns></returns>
public static DataSet ConvertXMLFileToDataSet(string xmlPath)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlPath);
DataSet xmlDS = new DataSet();
stream = new StringReader(xmld.InnerXml);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
//xmlDS.ReadXml(xmlFile);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
reader?.Close();
}
}
/// <summary>
/// 将DataSet转换为xml文件
/// </summary>
/// <param name="xmlDS"></param>
/// <param name="xmlPath">xml文件路径</param>
public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlPath)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
//返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlPath);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
writer?.Close();
}
}
}
}
/*
注意:XML是区分大小写的,节点名称是大写,属性是小写。这是规定,要不没办法解析
*/
C#
1
https://gitee.com/sundayisblue/code_snippet.git
git@gitee.com:sundayisblue/code_snippet.git
sundayisblue
code_snippet
代码片段
master

搜索帮助