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

本文关键字:节点 属性 反序列化 0项 xml | 更新日期: 2023-09-27 18:17:43

我这里有一个简化的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
还是

,问题是为什么是0 ?我错过什么了吗?

——update SOLVED——

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

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

这是因为您的item类中的属性atmosphere应该是一个名为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,你可以在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; }
    }
}

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