日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

.NET中XML序列化和反序列化常用類和屬性小結_實用技巧

作者:追逐時光者 ? 更新時間: 2022-06-04 編程語言

序列化和反序列化是指什么?

序列化(seriallization): 將對象轉化為便于傳輸的數據格式, 常見的序列化格式:二進制格式,字節數組,json字符串,xml字符串。
反序列化(deseriallization):將序列化的數據恢復為對象的過程。

XmlSerializer類

  該類用一種高度松散耦合的方式提供串行化服務。你的類不需要繼承特別的基類,而且它們也不需要實現特別的接口。相反,你只需在你的類或者這些類的公共域以及讀/寫屬性里加上自定義的特性。XmlSerializer通過反射機制讀取這些特性并用它們將你的類和類成員映射到xml元素和屬性(在對象和 XML 文檔之間進行序列化和反序列化操作)。

.NET-XML序列化和反序列化簡單示例代碼

using System.IO;
using System.Xml.Serialization;
 
namespace Practices.Common
{
    public class SimpleSerializer
    {
        /// 
        /// Model實體對象序列化為XML字符串
        /// 
        /// 對象類型
        /// Model實體對象
        /// 
        public static string SerializeXMLL(T t)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xmlSerializer = new XmlSerializer(t.GetType());
                xmlSerializer.Serialize(sw, t);
                return sw.ToString();
            }
        }
 
        /// 
        /// XML反序列化為對象
        /// 
        /// 對象類型
        /// xml字符串
        /// 
        public static T Deserialize(string xml)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            StringReader stringReader = new StringReader(xml);
            return (T)xmlSerializer.Deserialize(stringReader);
        }
 
    }
}

XmlTypeAttribute類

  ?該類主要控制當屬性目標由XML序列化時生成的XML節點。

應用示例

/// 
/// Envelope
/// 
[XmlType(TypeName = "envelope")]
public class CityRes
{
    public Header header { get; set; }
 
    public Response response { get; set; }
 
}

XmlElementAttribute類

?  該類用于指示公共字段或屬性在XML序列化或反序列化包含它們的對象時表示XML元素。

應用示例

/// 
/// Envelope
/// 
public class CityRes
{
    /// 
    /// header
    /// 
    [XmlElement("header")]
    public Header header { get; set; }
 
    /// 
    /// response
    /// 
    [XmlElement("response")]
    public Response response { get; set; }
 
}

XmlAttributeAttribute類

  該類指定XML序列化必須將類成員序列化為XML屬性。

應用示例

/// 
/// Version
/// 
public class Version
{
    /// 
    /// port
    /// 
    [XmlAttribute("port")]
    public string port { get; set; }
 
    /// 
    /// host
    /// 
    [XmlAttribute("host")]
    public string host { get; set; }
 
    /// 
    /// text
    /// 
    [XmlAttribute("text")]
    public string text { get; set; }
 
}

XmlArrayAttribute類

  該類主要用于XML元素數組的應用(相當于就是集合的聲明)。

應用示例

[XmlArray]
public Item []Items
{
   get{return items;}
   set{items = value;}
}

XmlTextAttribute類

  當Xml文檔序列化或反序列化時使用該特性修飾的成員會作為XML文本處理。

應用示例

[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
    get
    {
        return this.textField;
    }
    set
    {
        this.textField = value;
    }
}

XmlIgnoreAttribute類

?  指示該特性修飾的對象在Xml序列化時不會序列化該特性指定的元素。

應用示例

public class TeamGroup
{
 
   [XmlIgnore]
   public string Comment;
 
   public string GroupName;
}

用來控制XML序列化的屬性匯總

通過將下表中的特性應用于類和類成員,可以控制?XmlSerializer?序列化或反序列化該類的實例的方式。 若要了解這些屬性如何控制 XML 序列化,請參閱使用屬性控制 XML 序列化。

特性 適用對象 指定
XmlAnyAttributeAttribute 公共字段、屬性、參數或返回?XmlAttribute?對象數組的返回值。 反序列化時,將會使用?XmlAttribute?對象填充數組,而這些對象代表對于架構未知的所有 XML 特性。
XmlAnyElementAttribute 公共字段、屬性、參數或返回?XmlElement?對象數組的返回值。 反序列化時,將會使用?XmlElement?對象填充數組,而這些對象代表對于架構未知的所有 XML 元素。
XmlArrayAttribute 公共字段、屬性、參數或返回復雜對象的數組的返回值。 數組成員將作為 XML 數組的成員生成。
XmlArrayItemAttribute 公共字段、屬性、參數或返回復雜對象的數組的返回值。 可以插入數組的派生類型。 通常與?XmlArrayAttribute?一起應用。
XmlAttributeAttribute 公共字段、屬性、參數或返回值。 成員將作為 XML 屬性進行序列化。
XmlChoiceIdentifierAttribute 公共字段、屬性、參數或返回值。 可以使用枚舉進一步消除成員的歧義。
XmlElementAttribute 公共字段、屬性、參數或返回值。 字段或屬性將作為 XML 元素進行序列化。
XmlEnumAttribute 作為枚舉標識符的公共字段。 枚舉成員的元素名稱。
XmlIgnoreAttribute 公共屬性和公共字段。 序列化包含類時,應該忽略屬性或字段。
XmlIncludeAttribute 公共派生類聲明,以及 Web 服務描述語言 (WSDL) 文檔的公共方法的返回值。 生成要在序列化時識別的架構時,應該將該類包括在內。
XmlRootAttribute 公共類聲明。 控制視為 XML 根元素的屬性目標的 XML 序列化。 使用該屬性可進一步指定命名空間和元素名稱。
XmlTextAttribute 公共屬性和公共字段。 屬性或字段應該作為 XML 文本進行序列化。
XmlTypeAttribute 公共類聲明。 XML 類型的名稱和命名空間。

參考文章

C#: .net序列化及反序列化

用來控制XML序列化的屬性

使用屬性控制XML序列化

微軟官方文檔-XML相關類匯總

原文鏈接:https://www.cnblogs.com/Can-daydayup/p/16052873.html

欄目分類
最近更新