仅当ElementName与类名匹配时,XmlArray才会反序列化?

时间:2017-07-16 01:50:23

标签: c# xml-serialization xmlserializer

我遇到过一些非常奇怪的事情,如果有人能够解释这里发生的事情,我会很高兴。

这里的代码非常合适:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    [Serializable]
    [XmlRoot(ElementName = "Access")]
    public class Access
    {
        public Access()
        { 
        }

        [XmlElement(ElementName = "Else")]
        public string Else { get; set; }
        [XmlElement(ElementName = "Element")]
        public string Element { get; set; }
        [XmlArray(ElementName = "fields")]
        public Field[] Fields { get; set; }
    }

    [Serializable]
    [XmlRoot(ElementName = "Field")]
    public class Field
    {
        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }
        [XmlElement(ElementName = "Type")]
        public string Type { get; set; }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var data = @"<Access>
                        <Element>what is this ?!</Element>
                        <Else>432432elseasdas123213</Else>
                        <fields>
                            <Field><Name>Skype</Name><Type>Talking</Type></Field>
                            <Field><Name>IP Phone</Name><Type>Talking</Type></Field>
                            <Field><Name>PC</Name><Type>Still</Type></Field>
                            <Field><Name>Laptop</Name><Type>Mobile</Type></Field>
                        </fields>
                    </Access>";

            var serializer = new XmlSerializer(typeof(Access));

            Access access;

            using (var stream = new StringReader(data))
            using (var reader = XmlReader.Create(stream))
            {
                access = (Access)serializer.Deserialize(reader);

                Console.WriteLine("Fields");
                foreach (Field item in access.Fields)
                {
                    Console.WriteLine(item.Name);
                    Console.WriteLine(item.Type);
                }
            }
        }
    }
}

和这里的代码(几乎完全相同)但不能按预期工作:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    [Serializable]
    [XmlRoot(ElementName = "Access")]
    public class Access
    {
        public Access()
        { 
        }

        [XmlElement(ElementName = "Else")]
        public string Else { get; set; }
        [XmlElement(ElementName = "Element")]
        public string Element { get; set; }
        [XmlArray(ElementName = "fields")]
        public Field[] Fields { get; set; }
    }

    [Serializable]
    [XmlRoot(ElementName = "field")]
    public class Field
    {
        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }
        [XmlElement(ElementName = "Type")]
        public string Type { get; set; }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var data = @"<Access>
                        <Element>what is this ?!</Element>
                        <Else>432432elseasdas123213</Else>
                        <fields>
                            <field><Name>Skype</Name><Type>Talking</Type></field>
                            <field><Name>IP Phone</Name><Type>Talking</Type></field>
                            <field><Name>PC</Name><Type>Still</Type></field>
                            <field><Name>Laptop</Name><Type>Mobile</Type></field>
                        </fields>
                    </Access>";

            var serializer = new XmlSerializer(typeof(Access));

            Access access;

            using (var stream = new StringReader(data))
            using (var reader = XmlReader.Create(stream))
            {
                access = (Access)serializer.Deserialize(reader);

                Console.WriteLine("Fields");
                foreach (Field item in access.Fields)
                {
                    Console.WriteLine(item.Name);
                    Console.WriteLine(item.Type);
                }
            }
        }
    }
}

这两段代码之间的唯一区别是Field类上ElementName的大写,以及此类引用的XML中元素的大写。

  1. 到底是什么?我不能为了这个世界,弄清楚为什么会这样,有没有人有解释?
  2. 有没有人知道解决方法?
  3. 我宁愿不改变XML中的大小写以匹配ElementName。

    感谢。

0 个答案:

没有答案