簡體   English   中英

xml反序列化-我正在使用具有屬性的某些節點獲得0項

[英]xml deserialization - I am getting 0 item with some node that has attribute

我這里有一個簡化的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; }
    }
}

那是因為您的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 ,則可以使用[XmlArrayItem("part")]屬性(現在將成為List<atmosphere>類型[XmlArrayItem("part")]的屬性[XmlArrayItem("part")]進行操作,以便序列化程序知道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; }
    }
}

使用測試代碼時,這兩種方法都將為您提供預期的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM