4

yet another XML Deserialization question.

I have checked several other threads and tried most of the solutions there, but to no avail. The XML I receive can't be modded (or at least not easily) here it is:

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<ActueleVertrekTijden>
    <VertrekkendeTrein>
        <RitNummer>37047</RitNummer>
        <VertrekTijd>2012-11-13T15:40:00+0100</VertrekTijd>
        <EindBestemming>Sneek</EindBestemming>
        <TreinSoort>Stoptrein</TreinSoort>
        <Vervoerder>Arriva</Vervoerder>
        <VertrekSpoor wijziging=\"false\">3</VertrekSpoor>
    </VertrekkendeTrein>
    <VertrekkendeTrein>
        <RitNummer>10558</RitNummer>
        <VertrekTijd>2012-11-13T15:46:00+0100</VertrekTijd>
        <EindBestemming>Rotterdam Centraal</EindBestemming>
        <TreinSoort>Intercity</TreinSoort>
        <RouteTekst>Heerenveen, Steenwijk, Utrecht C</RouteTekst>
        <Vervoerder>NS</Vervoerder>
        <VertrekSpoor wijziging=\"false\">4</VertrekSpoor>
    </VertrekkendeTrein>
    <VertrekkendeTrein>
        <RitNummer>37349</RitNummer>
        <VertrekTijd>2012-11-13T15:59:00+0100</VertrekTijd>
        <EindBestemming>Groningen</EindBestemming>
        <TreinSoort>Sneltrein</TreinSoort>
        <RouteTekst>Buitenpost</RouteTekst>
        <Vervoerder>Arriva</Vervoerder>
        <VertrekSpoor wijziging=\"false\">5b</VertrekSpoor>
    </VertrekkendeTrein>
</ActueleVertrekTijden>

There are more elements (always a minumum of 10)

Now these are the classes I am deserializing too:

[Serializable, XmlRoot(ElementName="ActueleVertrekTijden", DataType="VertrekkendeTrein", IsNullable=false)]
public class ActueleVertrekTijden
{
    [XmlArray("ActueleVertrekTijden")]
    public VertrekkendeTrein[] VertrekLijst { get; set; }
}

[Serializable]
public class VertrekkendeTrein
{
    [XmlElement("RitNummer")]
    public string RitNummer { get; set; }
    [XmlElement("VertrekTijd")]
    public string VertrekTijd { get; set; }
    [XmlElement("EindBestemming")]
    public string EindBestemming { get; set; }
    [XmlElement("Vervoerder")]
    public string Vervoerder { get; set; }
    [XmlElement("VertrekSpoor")]
    public string VertrekSpoor { get; set; }
}

I omitted the others for the time being. The XmlRoot part I added because I got a "xmlsn="-error. So had to set the XmlRoot.

Now the Deserializer:

        public ActueleVertrekTijden Deserialize<ActueleVertrekTijden>(string s)
    {
        var ser = new XmlSerializer(typeof(ActueleVertrekTijden));
        ActueleVertrekTijden list = (ActueleVertrekTijden)ser.Deserialize(new StringReader(s));

        return list;
    }

It does return a ActueleVertrekTijden class but the VertrekLijst array remains null

3
  • I might be mistaken, but I don't think you want to set the DataType property of XmlRoot to that. Can you try only setting the ElementName? I don't think it will necessarily fix it, but just noticed it. Commented Nov 13, 2012 at 15:50
  • 3
    When I have issues trying to deserialize external XML, I actually try to serialize my class representations of it then compare it with the external XML I'm receiving. Often times there's a minor mixup or oversight/forgetfulness with how the .NET XmlSerializer works. Try creating a sample ActueleVertrekTijden with data with your application, serialize it to XML and compare its differences with your source XML that you're trying to deserialize. Commented Nov 13, 2012 at 15:52
  • That's a good idea (Now why didn't I think of that myself). Commented Nov 13, 2012 at 15:54

1 Answer 1

5

You need to omit the wrapper namespace, because your array elements are appearing directly below the container ActueleVertrekTijden class, without any collection wrapper element. i.e. change

 [XmlArray("ActueleVertrekTijden")]
 public VertrekkendeTrein[] VertrekLijst { get; set; }

to

 [XmlElement("VertrekkendeTrein")]
 public VertrekkendeTrein[] VertrekLijst { get; set; }

Reference here

Sign up to request clarification or add additional context in comments.

1 Comment

It worked, thanks. Like mentioned in the other thread: "How counter intuitive!" Wouldn't have figured this out myself very fast (perhaps after trial and error testing :P)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.