1

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

AmirDevouche
  • 45
  • 1
  • 6

3 Answers3

3

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; }
    }
jdweng
  • 33,250
  • 2
  • 15
  • 20
0
using System.Xml; //XmlDoc
using System.Xml.Linq;//XElement
using System.IO;//Path,File,Directory, Stream

Read and parse xml file:

XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(XmlFilePath);

Another approach is using XElement instead:

XElement a = XElement.Load(@"c:\path\file");

Most often I prefer XElement over XmlDocument, but that's personal

If you are starting with C#, you'd need a book, and a simpler project. Streams and Xml are syntactically complex. Also, console apps are ugly, and Forms apps are not that hard to do with the graphical tools of VisualStudio.

Roland
  • 4,619
  • 7
  • 49
  • 81
0

Your C# classes are not exactly aligned with the XML file and the serializer returns only a partial result. What you can do instead if the XML structure is fixed is outlined here.

https://stackoverflow.com/a/17315863/99804

This then works as you want it to.

You will get the following auto-generated code. Note: I have cleaned up the output to use auto-properties etc.

using System;
using System.Xml.Serialization;

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks />
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Network
{
    /// <remarks />
    [XmlArrayItem("ROUTE", IsNullable = false)]
    public NetworkROUTE[] ROUTES { get; set; }
}

[Serializable]
[XmlType(AnonymousType = true)]
public class NetworkROUTE
{
    [XmlAttribute]
    public string DIRECTION { get; set; }

    public string ENTRANCESIGNAL { get; set; }

    public string EXITSIGNAL { get; set; }

    [XmlAttribute]
    public string ID { get; set; }

    [XmlArrayItem("POINTENDID", IsNullable = false)]
    public NetworkROUTEPOINTENDID[] POINTENDIDS { get; set; }

    [XmlAttribute]
    public string ZONE { get; set; }
}

[Serializable]
[XmlType(AnonymousType = true)]
public class NetworkROUTEPOINTENDID
{
    [XmlAttribute]
    public string POS { get; set; }

    [XmlText]
    public string Value { get; set; }
}
bic
  • 2,201
  • 26
  • 27