最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解 C# 中XML對象的序列化和反序列化

 更新時間:2021年02月27日 15:36:21   作者:Hello 尋夢者!  
這篇文章主要介紹了詳解 C# 中XML對象的序列化和反序列化,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

這一篇主要是用來介紹關(guān)于C#中的XML序列化的問題,這個相信大家一定會經(jīng)常使用它,特別是在WPF中,有時候我們需要將我們后臺的數(shù)據(jù)保存在數(shù)據(jù)庫中,從而在軟件下一次啟動的時候能夠自動去加載這些數(shù)據(jù),由于我們的這些Model中字段眾多,如果單獨進(jìn)行保存那是不太現(xiàn)實的,這個時候?qū)⑦@些字段序列化成xml字符串并保存在數(shù)據(jù)庫中就是一個不錯的選擇,當(dāng)我們需要這些數(shù)據(jù)的時候我們也可以反過來將其序列化為一些字段,最終達(dá)到我們的效果,首先我們來看看是如何實現(xiàn)的?

public class XMLUtil
{
    /// <summary>
    /// XML & Datacontract Serialize & Deserialize Helper
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="serialObject"></param>
    /// <returns></returns>
    public static string Serializer<T>(T serialObject) where T : class
    {
        try
        {
            XmlSerializer ser = new XmlSerializer(typeof(T));
            System.IO.MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);
            ser.Serialize(writer, serialObject);
            writer.Close();
 
            return Encoding.UTF8.GetString(mem.ToArray());
        }
        catch (Exception ex)
        {
            return null;
        }
    }
 
    public static T Deserialize<T>(string str) where T : class
    {
        try
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            StreamReader mem2 = new StreamReader(
                    new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)),
                    System.Text.Encoding.UTF8);
 
            return (T)mySerializer.Deserialize(mem2);
        }
        catch (Exception)
        {
            return null;
        }
    }
    
}

  微軟為我們提供的XmlSerializer這個類就為我們提供了這個可能,在序列化的過程中我們需要注意下面的情況,所有的屬性必須是可序列化的對象,像BitmapImage、SolidColorBrush等這些對象都是不能夠進(jìn)行序列化的對象,如果用上面的方法進(jìn)行序列化時將返回null,正確的方式是在這些字段上面加上[XmlIgnore]說明,從而在進(jìn)行序列化時候跳過這些對象,就像下面的方式。

/// <summary>
///背景顏色
/// </summary>
private SolidColorBrush _BackgroundColor;
[XmlIgnore]
public SolidColorBrush BackgroundColor
{
    get
    {
        return _BackgroundColor;
    }
    set
    {
        if (value != _BackgroundColor)
        {
            _BackgroundColor = value;
            OnPropertyChanged("BackgroundColor");
        }
    }
}

  那么像上面的這個SolidColorBrush對象該怎樣去進(jìn)行序列化呢,這里我們選擇將當(dāng)前顏色的ARGB值保存在一個byte數(shù)組中,從而在反序列化的時候通過Color.FromArgb的方式進(jìn)行還原,就像下面的方式。

byte[] colorByte=savedModel.ConfigurationBaseModel.BackgroundColorArgb;
    Color backColor=Color.FromArgb(colorByte[0],colorByte[1],colorByte[2],colorByte[3]);
    sourceBaseModel.BackgroundColor = new SolidColorBrush(backColor);

  那么這個對象在進(jìn)行序列化的時候該怎么進(jìn)行保存呢?同樣的原理我們可以通過下面的方式。

/// <summary>
///背景顏色
/// </summary>
private SolidColorBrush _BackgroundColor;
[XmlIgnore]
public SolidColorBrush BackgroundColor
{
    get
    {
        return _BackgroundColor;
    }
    set
    {
        if (value != _BackgroundColor)
        {
            _BackgroundColor = value;
            OnPropertyChanged("BackgroundColor");
        }
    }
}
 
/// <summary>
///背景顏色ARGB
/// </summary>
private byte[] _BackgroundColorArgb=new byte[4];
[XmlArray("argb"),XmlArrayItem("value")]
public byte[] BackgroundColorArgb
{
    get
    {
        if (null != _BackgroundColor)
        {
            Color color = _BackgroundColor.Color;
            _BackgroundColorArgb[0] = color.A;
            _BackgroundColorArgb[1] = color.R;
            _BackgroundColorArgb[2] = color.G;
            _BackgroundColorArgb[3] = color.B;
        }
        return _BackgroundColorArgb;
    }
    set
    {
        if (value != _BackgroundColorArgb)
        {
            _BackgroundColorArgb = value;
            OnPropertyChanged("BackgroundColorArgb");
        }
     
    }
}

  這里在實際的使用中發(fā)現(xiàn),就像byte數(shù)組必須通過[XmlArray("argb"),XmlArrayItem("value")] 這類型的標(biāo)識來將其分類,在將其序列化完畢以后,我們可以看看這個BackgroundColorArgb字段最終是通過怎樣的方式來保存的?   

    在進(jìn)行反序列化的時候會將這個argb和value反序列化為一個byte數(shù)組。

    除了這些特例意外,有時候經(jīng)常需要將一些對象的數(shù)組進(jìn)行序列化,那么原理是什么呢?這里我借用別人的一個例子來進(jìn)行相關(guān)的說明。

 對象數(shù)組的Xml序列化:

 數(shù)組的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定數(shù)組元素的Xml節(jié)點名,XmlArrayItemAttribute指定數(shù)組元素的Xml節(jié)點名。

 如下代碼示例: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
  
namespace UseXmlSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            //聲明一個貓咪對象
            var cWhite = new Cat { Color = "White", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };
            var cBlack = new Cat { Color = "Black", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };
  
            CatCollection cc = new CatCollection { Cats = new Cat[] { cWhite,cBlack} };
  
            //序列化這個對象
            XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));
  
            //將對象序列化輸出到控制臺
            serializer.Serialize(Console.Out, cc);
  
            Console.Read();
        }
    }
  
    [XmlRoot("cats")]
    public class CatCollection
    {
        [XmlArray("items"),XmlArrayItem("item")]
        public Cat[] Cats { get; set; }
    }
  
    [XmlRoot("cat")]
    public class Cat
    {
        //定義Color屬性的序列化為cat節(jié)點的屬性
        [XmlAttribute("color")]
        public string Color { get; set; }
  
        //要求不序列化Speed屬性
        [XmlIgnore]
        public int Speed { get; set; }
  
        //設(shè)置Saying屬性序列化為Xml子元素
        [XmlElement("saying")]
        public string Saying { get; set; }
    }
}

  最終獲得的結(jié)果是:  

<?xml version="1.0" encoding="gb2312"?>
<cats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <items>
    <item color="White">
      <saying>White or black,  so long as the cat can catch mice,  it is a good cat</saying>
    </item>
    <item color="Black">
      <saying>White or black,  so long as the cat can catch mice,  it is a good cat</saying>
    </item>
  </items>
</cats> 

以上就是詳解 C# 中XML對象的序列化和反序列化的詳細(xì)內(nèi)容,更多關(guān)于c# xml序列化和反序列化的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入淺析Restful接口的兩種使用方式

    深入淺析Restful接口的兩種使用方式

    restful接口常用的兩種方式是get和post.接下來通過本文給大家介紹Restful接口的兩種使用方式,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2018-09-09
  • C#敏感詞過濾實現(xiàn)方法

    C#敏感詞過濾實現(xiàn)方法

    這篇文章主要介紹了C#敏感詞過濾實現(xiàn)方法,涉及C#針對字符串操作的常用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-05-05
  • C#利用NPOI操作Excel(單元格設(shè)置)

    C#利用NPOI操作Excel(單元格設(shè)置)

    這篇文章主要為大家詳細(xì)介紹了C#利用NPOI操作Excel實現(xiàn)單元格設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C#事件中的兩個參數(shù)詳解(object sender,EventArgs e)

    C#事件中的兩個參數(shù)詳解(object sender,EventArgs e)

    這篇文章主要介紹了C#事件中的兩個參數(shù)詳解(object sender,EventArgs e),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 最新評論

    历史| 汝阳县| 江源县| 玉龙| 留坝县| 阜城县| 稻城县| 鄂尔多斯市| 双城市| 石渠县| 皮山县| 体育| 湘潭县| 陇南市| 壤塘县| 水富县| 涪陵区| 旺苍县| 中山市| 北安市| 八宿县| 上思县| 丰顺县| 遵义县| 微山县| 庐江县| 通江县| 托克托县| 咸宁市| 嘉兴市| 子长县| 云林县| 依兰县| 木兰县| 泉州市| 龙川县| 呼图壁县| 凯里市| 丰原市| 西充县| 建昌县|