C#实现Xml序列化与反序列化的方法

作者:shichen2014 时间:2022-07-26 22:16:23 

本文实例讲述了C#实现Xml序列化与反序列化的方法。分享给大家供大家参考。具体实现方法如下:

/// <summary>
/// Xml序列化与反序列化
/// </summary>
public class XmlUtil
{
public static string GetRoot(string xml)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml.Replace("\r\n", "").Replace("\0", "").Trim());
    var e = doc.DocumentElement;
    return e.InnerText;
}

#region 反序列化
/// <summary>
/// 反序列化
/// </summary>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static T Deserialize<T>(string xml)
{
    return (T)Deserialize(typeof(T), xml);
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="stream">字节流</param>
/// <returns></returns>
public static T Deserialize<T>(Stream stream)
{
    return (T)Deserialize(typeof(T), stream);
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string xml)
{
    try
    {
 xml = xml.Replace("\r\n", "").Replace("\0", "").Trim();
 using (StringReader sr = new StringReader(xml))
 {
     XmlSerializer xmldes = new XmlSerializer(type);
     return xmldes.Deserialize(sr);
 }
    }
    catch (Exception e)
    {
 return null;
    }
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object Deserialize(Type type, Stream stream)
{
    XmlSerializer xmldes = new XmlSerializer(type);
    return xmldes.Deserialize(stream);
}
#endregion
#region 序列化
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer<T>(T obj)
{
    return Serializer(typeof(T), obj);
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer(Type type, object obj)
{
    MemoryStream Stream = new MemoryStream();
    XmlSerializerNamespaces _name = new XmlSerializerNamespaces();
    _name.Add("", "");//这样就 去掉 attribute 里面的 xmlns:xsi 和 xmlns:xsd
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
    xmlWriterSettings.Encoding = new UTF8Encoding(false);//设置编码,不能用Encoding.UTF8,会导致带有BOM标记
    xmlWriterSettings.Indent = true;//设置自动缩进
    //xmlWriterSettings.OmitXmlDeclaration = true;//删除XmlDeclaration:<?xml version="1.0" encoding="utf-16"?>
    //xmlWriterSettings.NewLineChars = "\r\n";
    //xmlWriterSettings.NewLineHandling = NewLineHandling.None;
    XmlSerializer xml = new XmlSerializer(type);
    try
    {
 using (XmlWriter xmlWriter = XmlWriter.Create(Stream, xmlWriterSettings))
 {
     //序列化对象
     xml.Serialize(xmlWriter, obj, _name);
 }
    }
    catch (InvalidOperationException)
    {
 throw;
    }
    return Encoding.UTF8.GetString(Stream.ToArray()).Trim();
}
#endregion
}

希望本文所述对大家的C#程序设计有所帮助。

标签:C#,Xml,方法
0
投稿

猜你喜欢

  • Android开发笔记之如何正确获取WebView的网页Title

    2022-06-04 07:02:41
  • SpringBoot之@Value获取application.properties配置无效的解决

    2023-09-15 22:37:55
  • C# 使用原生 System.IO.Compression 实现 zip 的压缩与解压

    2023-08-01 13:52:41
  • IntelliJ Idea常用11款插件(提高开发效率)

    2022-08-31 14:08:23
  • Android Jetpack组件中LifeCycle作用详细介绍

    2022-05-14 04:56:15
  • Android打开GPS导航并获取位置信息返回null解决方案

    2021-08-31 09:21:19
  • Unity工具类之生成文本验证码

    2021-06-21 03:38:42
  • ServletWebServerApplicationContext创建Web容器Tomcat示例

    2023-10-12 12:28:33
  • java文件操作工具类分享(file文件工具类)

    2023-11-24 22:32:47
  • Android选择图片或拍照图片上传到服务器

    2022-12-15 01:48:41
  • java中dart类详细讲解

    2022-01-23 13:01:13
  • 快速入门介绍Java中强大的String.format()

    2022-10-28 15:53:49
  • springcloud初体验(真香)

    2022-12-08 06:33:44
  • 解决Maven静态资源过滤问题

    2023-04-27 21:04:53
  • Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件

    2023-07-06 01:24:29
  • Android GPS获取当前经纬度坐标

    2021-10-31 12:09:40
  • SpringBoot整合Jackson超详细用法(附Jackson工具类)

    2023-05-31 23:56:06
  • 微信开发--企业转账到用户

    2023-01-06 00:52:41
  • c#使用Dataset读取XML文件动态生成菜单的方法

    2022-07-14 22:52:20
  • Java抽象类与接口区别详解

    2021-06-19 19:22:37
  • asp之家 软件编程 m.aspxhome.com