在反序列化过程中,XML节点为空
本文关键字:节点 XML 反序列化 过程中 | 更新日期: 2023-09-27 18:04:06
我有两个类,定义如下:
[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文件进行反序列化。
XMLFile :
<?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>
<<p> 反序列化方法/strong>: 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();
}
我可以得到"地板",但我不能得到名称和map_path节点值。我该怎么办?
XML文件的格式不适合XML序列化器读取。请遵循以下格式:
<?xml version="1.0" encoding="iso-8859-9"?>
<Topology>
<floors id="1">
<name>1</name>
<map_path>C:'</map_path>
</floors>
<floors id="2">
<name>1</name>
<map_path>C:'</map_path>
</floors>
</Topology>