xml反序列化 - 我得到0个项目,其中一个节点具有属性

时间:2013-06-09 21:20:29

标签: c# xml xml-serialization xmlserializer xml-deserialization

我在这里有一个精简的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; }
    }
}

1 个答案:

答案 0 :(得分:0)

这是因为atmosphere类中的属性item应该是名为part的类的列表,而不是atmosphere。该类应包含type属性和内部数据:

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")]类型)上的属性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; }
    }
}

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