AmirDevouche
AmirDevouche

Reputation: 45

XML serialization not getting expected xml file c#

I deserialized a xml file which looks like :

<?xml version="1.0" encoding="UTF-8"?>
<NETWORK>    
 <ROUTES>
  <ROUTE ID="RT_BALA_ORLS_R_111_119_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_ORLN_G119</EXITSIGNAL>
     <TRACKSECTIONIDS>
        <TRACKSECTIONID>BALA_ORLS_OSTK.TrackingObject</TRACKSECTIONID>
        <TRACKSECTIONID>BALA_ORLS_WBK_ORLN_EBK.TrackingObject</TRACKSECTIONID>
     </TRACKSECTIONIDS>
     <SUBROUTEIDS/>
     <POINTENDIDS>
        <POINTENDID POS="N">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
  <ROUTE ID="RT_BALA_ORLS_R_111_119D_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
     <ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
     <EXITSIGNAL>BALA_ORLN_G119D</EXITSIGNAL>
     <TRACKSECTIONIDS>
        <TRACKSECTIONID>BALA_ORLS_OSTK.TrackingObject</TRACKSECTIONID>
        <TRACKSECTIONID>BALA_ORLS_ORLN_Z164.TrackingObject</TRACKSECTIONID>
     </TRACKSECTIONIDS>
     <SUBROUTEIDS/>
     <POINTENDIDS>
        <POINTENDID POS="R">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
     </POINTENDIDS>
  </ROUTE>
 </ROUTES>
</NETWORK>

Now I'm trying to do some treatment on the deserialized data in order to do serialization and generate an xml file like this :

<NXRoutes>
   <NXRoute ID="1" OriginSignal="BALA_ORLS_G111" DestinationSignal="BALA_ORLN_G119">
      <Path ID="1" Preferred="YES" SnowPlan="NO">RT_BALA_ORLS_R_111_119_1</Path>
   </NXRoute>
   <NXRoute ID="2" OriginSignal="BALA_ORLS_G111" DestinationSignal="BALA_ORLN_G119D">
      <Path ID="1" Preferred="YES" SnowPlan="NO">RT_BALA_ORLS_R_111_119D_1</Path>
   </NXRoute>
</NXRoutes>

this is what I tried and I'm getting an empty xml file with only RootElement :

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();
        string OriginSignal = null;
        Console.WriteLine("");
        OriginSignal = Console.ReadLine();
        var SelectedRoutes = new List<ROUTE>();

        foreach (var route in XmlData.ROUTES)
        {
            if ((OriginSignal.Equals(route.ENTRANCESIGNAL)))
            {
                SelectedRoutes.Add(route);
            }   
        }
        //NXRoutes nxRoutes = new NXRoutes();
        List<NXRoute> NXRouteList = new List<NXRoute>();
        List<Path> paths = new List<Path>();
        Path path = new Path();
        NXRoute nxRoute = new NXRoute();
        NXRoutes nxRoutes = new NXRoutes();
        foreach (var item in SelectedRoutes)
        {

            nxRoute.ID = SelectedRoutes.Count.ToString();
            nxRoute.OriginSignal = OriginSignal;
            nxRoute.DestinationSignal = item.EXITSIGNAL;
            path.ID = "1";
            //nxRoute.Path.Add(path.ID);
            path.Preferred = "YES";
            path.SnowPlan = "NO";
            path.PathInnerText = item.ID;
            NXRouteList.Add(nxRoute);

            ///Console.WriteLine(item.ID);
        }
        //Console.ReadLine();    
        XmlSerializer serializer = new XmlSerializer(typeof(NXRoutes));
        System.IO.TextWriter writer = new StreamWriter(@"xml File Location");
        serializer.Serialize(writer, nxRoutes);
        writer.Close();
    }

when I started debugging, the object nxRoutesis null

there are the classes that I defined for Serialization

[XmlRoot("NXRoutes")]
public class NXRoutes
{
    [XmlElement("NXRoute")]
    public List<NXRoute> NXRoute { get; set; }
}

public class NXRoute
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
    [XmlAttribute("OriginSignal")]
    public string OriginSignal { get; set; }
    [XmlAttribute("DestinationSignal")]
    public string DestinationSignal { get; set; }
    [XmlElement("Path")]
    public List<Path> Path { get; set; }
}

public class Path
{
    [XmlAttribute("ID")]
    public string ID { get; set; }
    [XmlAttribute("Preferred")]
    public string Preferred { get; set; }
    [XmlAttribute("SnowPlan")]
    public string SnowPlan { get; set; }
    [XmlText]
    public string PathInnerText { get; set; }
}

And those for Deserialization :

[XmlRoot("NETWORK")]
public class Network
{
    [XmlArrayItem("ROUTE")]
    [XmlArray("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; }
}

being beginner in c# programming, can you help me to have the expected xml file

Upvotes: 2

Views: 71

Answers (1)

mjwills
mjwills

Reputation: 23974

I strongly suspect you are missing this line of code:

nxRoutes.NXRoute = NXRouteList;

You are currently serializing nxRoutes, but haven't actually initialised nxRoutes in any meaningful way.

Upvotes: 1

Related Questions