且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.NET(C#):在数组成员上加入XmlElement特性

更新时间:2022-10-04 15:31:40

原文 www.cnblogs.com/mgen/archive/2011/12/04/2276131.html

当对如下类进行XML序列化时:

publicclassa

{

    publicint[] arr =newint[] { 1, 2, 3 };

}

 

结果会是:

<axmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <arr>

        <int>1</int>

        <int>2</int>

        <int>3</int>

    </arr>

</a>

 

数组字段会成为单独的XML元素。

而如果在类型的数组成员上加入XmlElement特性,生成的XML不会有最外的字段名称XML元素,比如这样:

publicclassa

{

    [XmlElement(Type =typeof(int))]

    publicint[] arr =newint[] { 1, 2, 3 };

}

 

结果会生成这样的XML:

<axmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <arr>1</arr>

    <arr>2</arr>

    <arr>3</arr>

</a>

 

或许你在质疑这样的XML能不能被正确得反序列化?我开始也是这样想的,结果就是可以被成功反序列化,全部代码:

using System;

using System.Linq;

using System.Collections.Generic;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

 

namespace Mgen

{

    publicclassa

    {

        [XmlElement(Type =typeof(int))]

        publicint[] arr =newint[] { 1, 2, 3 };

    }

 

    classProgram

    {

        staticvoid Main()

        {

            var xs =newXmlSerializer(typeof(a));

            using (var textWriter =newStringWriter())

            {

                Console.WriteLine("序列化");

                xs.Serialize(textWriter, newa());

                Console.WriteLine(textWriter);

 

                Console.WriteLine("反序列化");

                var obj = (a)xs.Deserialize(newStringReader(textWriter.ToString()));

                foreach (var i in obj.arr)

                    Console.WriteLine(i);

 

            }

        }

    }

}

 

程序会输出:

序列化

<?xml version="1.0" encoding="utf-16"?>

<a xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <arr>1</arr>

  <arr>2</arr>

  <arr>3</arr>

</a>

反序列化

1

2

3

 

你可以添加多个XmlElement特性(如果数组会指向派生类的数组)。比如a的arr字段是一个object数组:

publicobject[] arr =newobject[] { 12, "hehe", Guid.NewGuid() };

 

输出XML:

<axmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <arr>

        <anyTypexsi:type="xsd:int">12</anyType>

        <anyTypexsi:type="xsd:string">hehe</anyType>

        <anyTypexmlns:q1="http://microsoft.com/wsdl/types/"

                 xsi:type="q1:guid">a4efc250-d935-4925-836e-d4b4b089e3fe</anyType>

    </arr>

</a>

注意object对应的XML元素是<anyType>,然后xsi:type属性是具体的类型。

 

如果在字段arr上加入XmlElement特性:

[XmlElement(Type =typeof(int)), XmlElement(Type =typeof(Guid)), XmlElement(Type =typeof(string))]

publicobject[] arr =newobject[] { 12, "hehe", Guid.NewGuid() };

 

输出XML:

<axmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <int>12</int>

    <string>hehe</string>

    <guid>58359cd3-afe3-4650-b222-9b3ff5df186a</guid>

</a>

作者:Mgen
出处:www.cnblogs.com/mgen
其他参考页面:我的软件和工程博客导读