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