欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

使用C#怎么對XML對象進行序列化和反序列化操作-創(chuàng)新互聯(lián)

使用C#怎么對XML對象進行序列化和反序列化操作?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)是專業(yè)的開封網(wǎng)站建設(shè)公司,開封接單;提供網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行開封網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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等這些對象都是不能夠進行序列化的對象,如果用上面的方法進行序列化時將返回null,正確的方式是在這些字段上面加上[XmlIgnore]說明,從而在進行序列化時候跳過這些對象,就像下面的方式。

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

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

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

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

/// <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字段最終是通過怎樣的方式來保存的?

使用C#怎么對XML對象進行序列化和反序列化操作

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

    除了這些特例意外,有時候經(jīng)常需要將一些對象的數(shù)組進行序列化,那么原理是什么呢?這里我借用別人的一個例子來進行相關(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> 

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

當(dāng)前標(biāo)題:使用C#怎么對XML對象進行序列化和反序列化操作-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.aaarwkj.com/article16/ccopgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、靜態(tài)網(wǎng)站網(wǎng)站維護、企業(yè)網(wǎng)站制作、建站公司、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
久久亚洲精品中文字幕一| 成人又黄又爽大片在线观看| 午夜精品一区二区三区久久| 亚洲各类熟女们中文字幕| 蜜桃av在线播放视频| 99久在线观看精品视频| av电影国产在线观看| 亚洲第一狼人天堂在线| 日本一区二区欧美在线| 亚洲av成人精品日韩一区麻豆 | 粉嫩国产精品一区二区| 日本中文字幕不卡在线一区二区| 久久久国产精品视频网站| 国产三级精品三级在线专区1| 日本视频免费一区二区| 亚洲日本香蕉视频观看视频| 免费黄色福利网址大片| 精品国产自在久久成人| 亚洲欧美午夜福利视频| 不卡的av中文字幕在线播放| 日韩精品一区伦理视频| 欧美黑人在线一区二区| 欧美精品一区二区网址| 肉肉开房天天操夜夜操| 综合久久—本道中文字幕| 欧美三级欧美一级视频看| 黄色日韩大片在线观看| 亚洲成年人黄片在线播放| 亚洲成人永久免费精品| 亚洲精品自拍一二三四区| 久久婷婷欧美激情综合| 色呦呦中文字幕在线播放| 日本最新一区二区三区视频| 日本区一区二区三高清视频| 亚洲精品成人一区二区| 小仙女精品经典三级永久| 色哟哟视频在线免费观看| 成人午夜激情在线观看| 国产欧美一区二区三区久久| 91麻豆亚洲国产成人久久精品| 精品久久久久久久中文字幕|