程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成Xml序列化與反序列化的辦法

C#完成Xml序列化與反序列化的辦法

編輯:C#入門知識

C#完成Xml序列化與反序列化的辦法。本站提示廣大學習愛好者:(C#完成Xml序列化與反序列化的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成Xml序列化與反序列化的辦法正文


本文實例講述了C#完成Xml序列化與反序列化的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

/// <summary>
/// Xml序列化與反序列化
/// </summary>
public class XmlUtil
{
public static string GetRoot(string xml)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml.WordStr("\r\n", "").WordStr("\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.WordStr("\r\n", "").WordStr("\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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved