0

我有如下定义的 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 节点值。我该怎么办?

4

1 回答 1

1

您的 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>
于 2013-03-15T09:51:31.583 回答