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

C# xml反序列化-我得到0项,其中某个节点具有属性,c#,xml,xml-serialization,xmlserializer,xml-deserialization,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">

我这里有一个精简的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.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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Xml.Serialization;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=“planet\u oneplanet\u two”;
使用(StreamReader=newstreamreader(“d:\\test.xml”))
{
xml=reader.ReadToEnd();
}
var serialiser=新的XmlSerializer(typeof(node1));
var streamXML=new System.IO.StringReader(xml);
var output=serialiser.Deserialize(streamXML);
节点1 iData=(节点1)输出;
foreach(iData.items中的项目n)
{
Console.WriteLine(n.displayname);
Console.WriteLine(“大气计数:+n.atmosphere.parts.Length”);
foreach(n.atmosphere.parts中的字符串a)
{
控制台写入线(“+a”);
}
Console.WriteLine(“-----------\r\n”);
}
控制台。写入线(“完成”);
Console.ReadLine();
}
}
公共类节点1
{
公共项[]项{get;set;}
}
公共课堂气氛
{
[XmlElement(“部分”)]
公共字符串[]部分{get;set;}
}
公共类项目
{
公共字符串displayname{get;set;}
公共气氛{get;set;}
}
}

这是因为
类中的属性
大气
应该是名为
部分
的类的列表,而不是
大气
。该类应包含类型属性和内部数据:

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; }
    }
}

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

在您发布解决方案之前,我已经解决了这个问题。但作为对你们努力的一份礼物,我会给你们一个答案。
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; }
    }
}
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; }
    }
}
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; }
    }
}