sanchop22
sanchop22

Reputation: 2809

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?

Upvotes: 0

Views: 138

Answers (1)

DerApe
DerApe

Reputation: 3175

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>

Upvotes: 1

Related Questions