欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入分析XmlSerializer對(duì)象的Xml序列化與反序列化的示例詳解

 更新時(shí)間:2013年05月18日 17:14:15   作者:  
本篇文章是對(duì)XmlSerializer 對(duì)象的Xml序列化與反序列化的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
這篇隨筆對(duì)應(yīng)的.Net命名空間是System.Xml.Serialization;文中的示例代碼需要引用這個(gè)命名空間。
為什么要做序列化和反序列化?
.Net程序執(zhí)行時(shí),對(duì)象都駐留在內(nèi)存中;內(nèi)存中的對(duì)象如果需要傳遞給其他系統(tǒng)使用;或者在關(guān)機(jī)時(shí)需要保存下來(lái)以便下次再次啟動(dòng)程序使用就需要序列化和反序列化。
范圍:本文只介紹xml序列化,其實(shí)序列化可以是二進(jìn)制的序列化,也可以是其他格式的序列化。
看一段最簡(jiǎn)單的Xml序列化代碼
復(fù)制代碼 代碼如下:

class Program
{
    static void Main(string[] args)
    {
        int i = 10;
        //聲明Xml序列化對(duì)象實(shí)例serializer
        XmlSerializer serializer = new XmlSerializer(typeof(int));
        //執(zhí)行序列化并將序列化結(jié)果輸出到控制臺(tái)
        serializer.Serialize(Console.Out, i);
        Console.Read();
    }
}

上面代碼對(duì)int i進(jìn)行了序列化,并將序列化的結(jié)果輸出到了控制臺(tái),輸出結(jié)果如下
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="gb2312"?>
<int>10</int>

可以將上述序列化的xml進(jìn)行反序列化,如下代碼
復(fù)制代碼 代碼如下:

static void Main(string[] args)
{
    using (StringReader rdr = new StringReader(@"<?xml version=""1.0"" encoding=""gb2312""?>
<int>10</int>"))
    {
        //聲明序列化對(duì)象實(shí)例serializer
        XmlSerializer serializer = new XmlSerializer(typeof(int));
        //反序列化,并將反序列化結(jié)果值賦給變量i
        int i = (int)serializer.Deserialize(rdr);
        //輸出反序列化結(jié)果
        Console.WriteLine("i = " + i);
        Console.Read();
    }
}

以上代碼用最簡(jiǎn)單的方式說(shuō)明了xml序列化和反序列化的過(guò)程,.Net系統(tǒng)類(lèi)庫(kù)為我們做了大量的工作,序列化和反序列化都非常簡(jiǎn)單。但是在現(xiàn)實(shí)中業(yè)務(wù)需求往往比較復(fù)雜,不可能只簡(jiǎn)單的序列化一個(gè)int變量,顯示中我們需要對(duì)復(fù)雜類(lèi)型進(jìn)行可控制的序列化。
自定義對(duì)象的Xml序列化:
System.Xml.Serialization命名空間中有一系列的特性類(lèi),用來(lái)控制復(fù)雜類(lèi)型序列化的控制。例如XmlElementAttribute、XmlAttributeAttribute、XmlArrayAttribute、XmlArrayItemAttribute、XmlRootAttribute等等。
看一個(gè)小例子,有一個(gè)自定義類(lèi)Cat,Cat類(lèi)有三個(gè)屬性分別為Color,Saying,Speed。
復(fù)制代碼 代碼如下:

namespace UseXmlSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            //聲明一個(gè)貓咪對(duì)象
            var c = new Cat { Color = "White", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };

            //序列化這個(gè)對(duì)象
            XmlSerializer serializer = new XmlSerializer(typeof(Cat));

            //將對(duì)象序列化輸出到控制臺(tái)
            serializer.Serialize(Console.Out, c);

            Console.Read();
        }
    }
    [XmlRoot("cat")]
    public class Cat
    {
        //定義Color屬性的序列化為cat節(jié)點(diǎn)的屬性
        [XmlAttribute("color")]
        public string Color { get; set; }

        //要求不序列化Speed屬性
        [XmlIgnore]
        public int Speed { get; set; }

        //設(shè)置Saying屬性序列化為Xml子元素
        [XmlElement("saying")]
        public string Saying { get; set; }
    }
}

可以使用XmlElement指定屬性序列化為子節(jié)點(diǎn)(默認(rèn)情況會(huì)序列化為子節(jié)點(diǎn));或者使用XmlAttribute特性制定屬性序列化為Xml節(jié)點(diǎn)的屬性;還可以通過(guò)XmlIgnore特性修飾要求序列化程序不序列化修飾屬性。
對(duì)象數(shù)組的Xml序列化:
數(shù)組的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定數(shù)組元素的Xml節(jié)點(diǎn)名,XmlArrayItemAttribute指定數(shù)組元素的Xml節(jié)點(diǎn)名。
如下代碼示例:
復(fù)制代碼 代碼如下:

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)
        {
            //聲明一個(gè)貓咪對(duì)象
            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} };

            //序列化這個(gè)對(duì)象
            XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));

            //將對(duì)象序列化輸出到控制臺(tái)
            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é)點(diǎn)的屬性
        [XmlAttribute("color")]
        public string Color { get; set; }

        //要求不序列化Speed屬性
        [XmlIgnore]
        public int Speed { get; set; }

        //設(shè)置Saying屬性序列化為Xml子元素
        [XmlElement("saying")]
        public string Saying { get; set; }
    }
}

以上代碼將輸出:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="gb2312"?>
<cats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://ww
w.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>

XmlSerializer內(nèi)存泄漏問(wèn)題:
仔細(xì)看了下msdn,確實(shí)存在泄漏的情況,msdn說(shuō)明如下:
動(dòng)態(tài)生成的程序集
為了提高性能,XML 序列化基礎(chǔ)結(jié)構(gòu)將動(dòng)態(tài)生成程序集,以序列化和反序列化指定類(lèi)型。此基礎(chǔ)結(jié)構(gòu)將查找并重復(fù)使用這些程序集。此行為僅在使用以下構(gòu)造函數(shù)時(shí)發(fā)生:
XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)
如果使用任何其他構(gòu)造函數(shù),則會(huì)生成同一程序集的多個(gè)版本,且絕不會(huì)被卸載,這將導(dǎo)致內(nèi)存泄漏和性能降低。最簡(jiǎn)單的解決方案是使用先前提到的兩個(gè)構(gòu)造函數(shù)的其中一個(gè)。否則,必須在 Hashtable 中緩存程序集,如以下示例中所示。
也就是說(shuō)我們?cè)谑褂肵mlSerializer序列化,初始化XmlSerializer對(duì)象時(shí)最好使用下面兩個(gè)構(gòu)造函數(shù)否則會(huì)引起內(nèi)存泄漏。
XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)

相關(guān)文章

  • ASP.net中md5加密碼的方法

    ASP.net中md5加密碼的方法

    ASP.net中md5加密碼的方法...
    2006-07-07
  • ASP.NET學(xué)習(xí)中常見(jiàn)錯(cuò)誤總結(jié)歸納

    ASP.NET學(xué)習(xí)中常見(jiàn)錯(cuò)誤總結(jié)歸納

    這篇文章主要介紹了asp.net學(xué)習(xí)過(guò)程中碰到的常見(jiàn)錯(cuò)誤的解決方法,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-09-09
  • ASP.NET Core 依賴(lài)注入詳細(xì)

    ASP.NET Core 依賴(lài)注入詳細(xì)

    這篇文章主要介紹ASP.NET Core 依賴(lài)注入,ASP.NET Core 應(yīng)用在啟動(dòng)過(guò)程中會(huì)依賴(lài)各種組件提供服務(wù),這些組件會(huì)以接口的形式標(biāo)準(zhǔn)化,這些組件就是我們所說(shuō)的服務(wù),ASP.NET Core框架建立在一個(gè)底層的依賴(lài)注入框架之上,它使用容器提供所需的服務(wù),下面我們就來(lái)詳細(xì)了解一下
    2021-10-10
  • .Net?Core?配置文件讀取IOptions,IOptionsMonitor,IOptionsSnapshot

    .Net?Core?配置文件讀取IOptions,IOptionsMonitor,IOptionsSnapshot

    這篇文章主要介紹了.Net?Core配置文件讀取IOptions,IOptionsMonitor,IOptionsSnapshot,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 在?ASP.NET?Core?中為?gRPC?服務(wù)添加全局異常處理

    在?ASP.NET?Core?中為?gRPC?服務(wù)添加全局異常處理

    這篇文章主要介紹了在?ASP.NET?Core?中為?gRPC?服務(wù)添加全局異常處理?,在?ASP.NET?Core?中使用?GRPC.ASPNETCore?工具包寫(xiě)?gRPC?服務(wù),想實(shí)現(xiàn)?gRPC?的異常全局?jǐn)r截,下面一起來(lái)看看文中的詳細(xì)內(nèi)容吧
    2022-01-01
  • ASP.NET Core WebSocket集群實(shí)現(xiàn)思路詳解

    ASP.NET Core WebSocket集群實(shí)現(xiàn)思路詳解

    這篇文章主要為大家介紹了ASP.NET Core WebSocket集群實(shí)現(xiàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • ASP遺留的二十個(gè)積習(xí)

    ASP遺留的二十個(gè)積習(xí)

    ASP遺留的二十個(gè)積習(xí)
    2006-07-07
  • .NET新能源汽車(chē)鋰電池檢測(cè)程序UI掛死問(wèn)題分析

    .NET新能源汽車(chē)鋰電池檢測(cè)程序UI掛死問(wèn)題分析

    這篇文章主要為大家介紹了.NET新能源汽車(chē)鋰電池檢測(cè)程序UI掛死問(wèn)題分析?,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • ASP.NET Core 中間件的使用之全局異常處理機(jī)制

    ASP.NET Core 中間件的使用之全局異常處理機(jī)制

    我們今天這篇文章就來(lái)說(shuō)說(shuō)代碼異常問(wèn)題怎么快速定位,減少不必要的時(shí)間浪費(fèi)。異常是一種運(yùn)行時(shí)錯(cuò)誤,當(dāng)異常沒(méi)有得到適當(dāng)?shù)奶幚?,很可能?huì)導(dǎo)致你的程序意外終止。下面雄安邊將詳細(xì)介紹,需要的朋友可以參考下
    2021-09-09
  • HttpResponse的Output與OutputStream、Filter關(guān)系與區(qū)別介紹

    HttpResponse的Output與OutputStream、Filter關(guān)系與區(qū)別介紹

    在網(wǎng)上經(jīng)常看見(jiàn)有這樣的代碼HttpResponse response = HttpContext.Current.Response;現(xiàn)在我也來(lái)說(shuō)說(shuō)這幾個(gè)東東是什么吧
    2012-11-11

最新評(píng)論