Search code examples
c#xmlxml-deserialization

Deserialize a xml file with child elements in c#


I am trying to Deserialize a xml file of this type

<?xml version="1.0" encoding="UTF-8"?>
<Network>
 <ROUTES>
  <ROUTE ID="RT_BALA_GLNC_R_162_154_1" DIRECTION="LEFT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_GLNC_G162</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_DONS_G154</EXITSIGNAL>
     <POINTENDIDS>
        <POINTENDID POS="N">PT_BALA_GLNC_W11.TrackPortionConnection</POINTENDID>
        <POINTENDID POS="N">PT_BALA_GLNC_W23.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
  <ROUTE ID="RT_BALA_ORLS_R_111_119_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_ORLN_G119</EXITSIGNAL>
     <POINTENDIDS>
        <POINTENDID POS="N">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
  <ROUTE ID="RT_BALA_GLNC_R_162D_154_1" DIRECTION="LEFT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_GLNC_G162D</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_DONS_G154</EXITSIGNAL>
     <POINTENDIDS>
        <POINTENDID POS="R">PT_BALA_GLNC_W11.TrackPortionConnection</POINTENDID>
        <POINTENDID POS="N">PT_BALA_GLNC_W23.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
 </ROUTES>
</Network>

I have tried this

class Program
{
    static void Main(string[] args)
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(Network));
        TextReader reader = new StreamReader(@"xml File Location");
        object obj = deserializer.Deserialize(reader);
        Network XmlData = (Network)obj;
        reader.Close();
        Console.ReadLine();
    }
}
[XmlRoot("Network")]
public class Network
{
    [XmlElement("ROUTES")]
    public List<ROUTE> ROUTES { get; set; }
}

public class ROUTE
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
    [XmlAttribute("DIRECTION")]
    public string DIRECTION { get; set; }
    [XmlElement("ENTRANCESIGNAL")]
    public string ENTRANCESIGNAL { get; set; }
    [XmlElement("EXITSIGNAL")]
    public string EXITSIGNAL { get; set; }
    [XmlElement("POINTENDIDS")]
    public POINTENDIDS POINTENDIDS { get; set; }
}

public class POINTENDIDS
{
    [XmlElement("POINTENDID")]
    public List<POINTENDID> POINTENDID { get; set; }
}

public class POINTENDID
{
    [XmlAttribute("POS")]
    public string POS { get; set; }
}

I am doing it in a console application,

I started Debugging and put breakpoint on Network XmlData = (Network)obj;

I've got only 1 ROUTES and the values of "ID", "DIRECTION", "ENTRANCESIGNAL" ...etc are set to Null

being beginner in c# programming , I don't really understand what should I do !

Need help for this implementation


Solution

  • Fix you Network Class. The names in square brackets are case sensitive. You also need to add the Xml array attributes.

        [XmlRoot("Network")]
        public class Network
        {
            [XmlArrayItem("ROUTE")]
            [XmlArray("ROUTES")]
            public List<ROUTE> ROUTES { get; set; }
        }