[英]xml nodes are null while deserialization process
I have 2 classes as defined below: 我有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; }
}
I want to deserialize the xml file shown below and i use the below specified method to deserialize the xml file. 我想反序列化下面显示的xml文件,我使用下面指定的方法反序列化xml文件。
XMLFile : 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>
Deserialize Method : 反序列化方法 :
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();
}
I can get "Floors" but i couldn't get the name and map_path node values. 我可以获取“ Floors”,但无法获取名称和map_path节点值。 What should i do?
我该怎么办?
Your XML file is not properly formatet for the xml serializer to read. 您的XML文件未正确格式化以供xml序列化程序读取。 Please follow the following formating:
请遵循以下格式:
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.