简体   繁体   中英

xml nodes are null while deserialization process

I have 2 classes as defined below:

[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.

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. What should i do?

Your XML file is not properly formatet for the xml serializer to read. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM