1

我在这里有一个精简的 XML 示例

<?xml version="1.0" encoding="utf-8"?>
<node1>
    <items>
        <item>
            <displayname>planet_one</displayname>
            <atmosphere>
                <part type="value">
                    <![CDATA[Hydrogen]]>
                </part>
            </atmosphere>
        </item>
        <item>
            <displayname>planet_two</displayname>
            <atmosphere>
                <part type="value">
                    <![CDATA[Hydrogen]]>
                </part>
                <part type="value">
                    <![CDATA[Sulfur]]>
                </part>
                <part type="value">
                    <![CDATA[Acid]]>
                </part>
            </atmosphere>
        </item>
    </items>
</node1>

我不确定反序列化后,我一直得到零大气?我不知道为什么,我尝试更改我的代码,但我仍然保持零。这是供您测试的测试代码。

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><node1><items><item><displayname>planet_one</displayname><atmosphere><part type=\"value\"><![CDATA[Hydrogen]]></part></atmosphere></item><item><displayname>planet_two</displayname><atmosphere><part type=\"value\"><![CDATA[Hydrogen]]></part><part type=\"value\"><![CDATA[Sulfur]]></part><part type=\"value\"><![CDATA[Acid]]></part></atmosphere></item></items></node1>";

            var serialiser = new XmlSerializer(typeof(node1));
            var streamXML = new System.IO.StringReader(xml);
            var output = serialiser.Deserialize(streamXML);

            node1 iData = (node1)output;

            foreach (item n in iData.items)
            {
                Console.WriteLine(n.displayname);
                Console.WriteLine("atmospheres count: " + n.atmosphere.Count);
            }

            Console.WriteLine("done");
            Console.ReadLine();
        }
    }

    public class node1
    {
        List<item> _items = new List<item>();
        public List<item> items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
            }
        }
    }

    public class atmosphere
    {
        public string part { get; set; }
    }

    public class item
    {
        public string displayname { get; set; }

        List<atmosphere> _atmosphere = new List<atmosphere>();
        public List<atmosphere> atmosphere
        {
            get
            {
                return _atmosphere;
            }
            set
            {
                _atmosphere = value;
            }
        }
    }
}

这里的输出是

planet_one
atmospheres count: 0
planet_two
atmospheres count: 0
done

再次,问题是为什么为零?我错过了什么吗?

-- 更新已解决 --

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><node1><items><item><displayname>planet_one</displayname><atmosphere><part type=\"value\"><![CDATA[Hydrogen]]></part></atmosphere></item><item><displayname>planet_two</displayname><atmosphere><part type=\"value\"><![CDATA[Hydrogen]]></part><part type=\"value\"><![CDATA[Sulfur]]></part><part type=\"value\"><![CDATA[Acid]]></part></atmosphere></item></items></node1>";

            using (StreamReader reader = new StreamReader("d:\\test.xml"))
            {
                xml = reader.ReadToEnd();
            }

            var serialiser = new XmlSerializer(typeof(node1));
            var streamXML = new System.IO.StringReader(xml);
            var output = serialiser.Deserialize(streamXML);

            node1 iData = (node1)output;

            foreach (item n in iData.items)
            {
                Console.WriteLine(n.displayname);
                Console.WriteLine("atmospheres count: " + n.atmosphere.parts.Length);
                foreach (string a in n.atmosphere.parts)
                {
                    Console.WriteLine("    " + a);
                }

                Console.WriteLine("-------------\r\n");
            }

            Console.WriteLine("done");
            Console.ReadLine();
        }
    }

    public class node1
    {
        public item[] items { get; set; }
    }

    public class atmosphere
    {
        [XmlElement("part")]
        public string[] parts { get; set; }
    }

    public class item
    {
        public string displayname { get; set; }

        public atmosphere atmosphere { get; set; }
    }
}
4

1 回答 1

0

那是因为atmosphere你的item类中的属性应该是一个名为part而不是atmosphere. 该类应包含类型属性和内部数据:

public class item
{
    public string displayname { get; set; }

    List<part> _atmosphere = new List<part>();
    public List<part> atmosphere
    {
        get
        {
            return _atmosphere;
        }
        set
        {
            _atmosphere = value;
        }
    }
}

public class part
{
    [XmlAttribute]
    public string type { get; set; }

    string _data;
    [XmlText]
    public string Data
    {
        get { return _data; }
        set { _data = value; }
    }
}

如果您希望保存元素的类继续被命名atmosphere,您可以使用[XmlArrayItem("part")]大气属性(现在将是 type List<atmosphere>)上的属性来做到这一点,这样序列化程序就知道 xml 中元素的名称是“part”而不是“atmosphere”(因为默认情况下它会期望元素的名称与类的名称相同):

public class item
{
    public string displayname { get; set; }

    List<atmosphere> _atmosphere = new List<atmosphere>();
    [XmlArrayItem("part")]
    public List<atmosphere> atmosphere
    {
        get
        {
            return _atmosphere;
        }
        set
        {
            _atmosphere = value;
        }
    }
}

public class atmosphere
{
    [XmlAttribute]
    public string type { get; set; }

    string _data;
    [XmlText]
    public string Data
    {
        get { return _data; }
        set { _data = value; }
    }
}

使用测试代码时,这两种方法都会为您提供预期的结果。

于 2013-06-09T21:49:33.277 回答