1
votes

Désérialiser un fichier xml avec des éléments enfants en c #

J'essaye de désérialiser un fichier xml de ce type

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; }
}

J'ai essayé ceci

<?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>

Je le fais dans un application console,

J'ai commencé le débogage et mis un point d'arrêt sur Network XmlData = (Network) obj;

J'ai seulement 1 ROUTES et les valeurs de "ID", "DIRECTION" , "ENTRANCESIGNAL" ... etc sont mis à Null

étant débutant en programmation c #, je ne comprends pas vraiment ce que je dois faire!

Besoin d'aide pour cette implémentation p>


1 commentaires

Sensible aux majuscules et minuscules. Utilisez [XmlRoot ("Network")] .


3 Réponses :


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

0 commentaires

2
votes

Réparez votre classe de réseau. Les noms entre crochets sont sensibles à la casse. Vous devez également ajouter les attributs du tableau Xml.

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


1 commentaires

merci pour votre réponse, maintenant j'obtiens les résultats attendus :)



0
votes

Vos classes C # ne sont pas exactement alignées avec le fichier XML et le sérialiseur ne renvoie qu'un résultat partiel. Ce que vous pouvez faire à la place si la structure XML est corrigée est décrit ici.

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

Cela fonctionne alors comme vous le souhaitez.

Vous obtiendrez le code généré automatiquement suivant. Remarque: j'ai nettoyé la sortie pour utiliser les propriétés automatiques, 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; }
}


0 commentaires