我有如下定义的 2 个类:
[Serializable()]
public class Topology
{
[XmlElement("floors")]
public Floor[] Floors { get; set; }
}
[Serializable()]
public class Floor
{
[XmlElement("name")]
public string name { get; set; }
[XmlElement("map_path")]
public string map_path { get; set; }
}
我想反序列化下面显示的 xml 文件,我使用下面指定的方法来反序列化 xml 文件。
XML文件:
<?xml version="1.0" encoding="iso-8859-9"?>
<Topology>
<floors>
<floor id="1">
<name>1</name>
<map_path>C:\</map_path>
</floor>
<floor id="2">
<name>2</name>
<map_path>D:\</map_path>
</floor>
</floors>
</Topology>
反序列化方法:
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(Topology));
StreamReader reader = new StreamReader(@"C:\topology2.xml");
Topology top = (Topology)serializer.Deserialize(reader);
reader.Close();
for (int i = 0; i < top.Floors.Length; i++ )
Console.WriteLine(top.Floors[i].name + top.Floors[i].map_path);
Console.ReadLine();
}
我可以获得“Floors”,但我无法获得名称和 map_path 节点值。我该怎么办?