2

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

AmirDevouche
  • 45
  • 1
  • 6
  • Use a debugger and see what `nxRoutes` contains. We can´t know how your objects look like. That having said please provide a [**Minimal**, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – MakePeaceGreatAgain Jan 24 '19 at 09:21
  • I'm getting `nxRoutes` null – AmirDevouche Jan 24 '19 at 09:27
  • 1
    How can it be `null` when you assign `new NXRoutes()` to it? – MakePeaceGreatAgain Jan 24 '19 at 09:31
  • You newed up `nxRoutes` and then did nothing to it. Can you talk us through what the difference between `nxRoutes` and `NXRouteList` is? – mjwills Jan 24 '19 at 09:40
  • `nxRoutes` is an instance of a class `NXRoute` and `NXRouteList` is an instance of a list declared as `XmlElement` in the class `NXRoutes` declared as `XmlRoot` – AmirDevouche Jan 24 '19 at 10:10
  • @mjwills can you please tell me how I can reach the 'path' list's elements in order to assign values to the attributes (ID, Preferred ...etc) ?. I'm trying to do it with foreach loop but I'm getting this exception `System.NullReferenceException: 'Object reference not set to an instance of an object.' ReadXmlFile.NXRoute.Path.get returned null` – AmirDevouche Jan 25 '19 at 10:24

1 Answers1

1

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.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • 1
    Yes now I understand why, so I also moved the declaration inside the loop to get a fresh object on each pass of the foreach loop: `NXRoute nxRoute = new NXRoute();` But I don't know how to reach the element `` and it's attributes , it seems like a list inside a list ... In my generated xml file `` element is missing – AmirDevouche Jan 24 '19 at 15:24