Valentin Martin
Valentin Martin

Reputation: 11

How can I deserialize an XML - Windows Phone

I'm using this function to deserialise the data of the xml file:

Uri uri = new Uri("/XML/ligne1_EUROPE.xml", UriKind.Relative);
        XDocument document = XDocument.Load("XML/ligne1_EUROPE.xml"); 
        XmlSerializer serializer = new XmlSerializer(typeof(Destinataires));
        Destinataires ArretLoad = (Destinataires)serializer.Deserialize(document.CreateReader());
        listBox.ItemsSource = ArretLoad.Collection;

He call the Destinataires class:

[XmlRoot("root")]
public class Destinataires
{
    [XmlArray("Destinataires")]
    [XmlArrayItem("Destinataire")]

    [XmlArrayItem("Horraires")]
    [XmlArrayItem("Horraire")]

    public ObservableCollection<XML_Arret> Collection { get; set; }
}

And call the XML_Arret class:

public class XML_Arret
{
    [XmlElement("Designation")]
    public string Designation { get; set; }

    [XmlElement("Carre")]
    public string Carre { get; set; }

    [XmlElement("Horraires")]
    public Horraires[] Horaires { get; set; }

    public class Horraires
    {
        [XmlElement("Horraire")]
        public string Horraire { get; set; }
    }
}

My XML files is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <Destinataires>
    <Destinataire>
      <Designation>Faubourd d'isle</Designation>
      <Carre>images/1.png</Carre>
      <Horraires>
        <Horraire>6h18</Horraire>
        <Horraire>6h28</Horraire>
        <Horraire>6h38</Horraire>
          ...
      </Horraires>
    </Destinataire>
        ...
  </Destinataires
</root>

But when I debug my programm, I've this message: Une exception de type 'System.InvalidOperationException' s'est produite dans System.Xml.Serialization.ni.dll mais n'a pas été gérée dans le code utilisateur at the line: XmlSerializer serializer = new XmlSerializer(typeof(Destinataires));

So, I'm lost, Do you can Help me?

And I'm sorry for my langage, I'm a french person.

Thanks


I've change my code for:

[XmlRoot("root")]
public class Destinataires
{
    [XmlArray("Destinataires")]
    [XmlArrayItem("Destinataire")]

    public ObservableCollection<XML_Arret> Collection { get; set; }
}

But I've a problem:

I use this function:

private void ArretList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var app = App.Current as App;
        app.selectArret = (XML_Arret) listBox.SelectedItem;
        var arret = app.selectArret.Designation;
        MessageBox.Show(arret);
        this.NavigationService.Navigate(new Uri("/BUS_InfoArret.xaml?Arret=" + arret, UriKind.Relative));
    }

In my app.xaml.cs I've:

 public XML_Arret selectArret { get; set; }

And In my BUS_InfoArret: XML_Arret arret; public BUS_InfoArret() { InitializeComponent();

        var app = App.Current as App;
        arret = app.selectArret;

        Designation.Text = arret.Designation.ToString(); <-- Work

        for (int i = 0; i < arret.Horaires.Length;  ++i) <-- Display Ville_st_quentin.XML_Arret+Horraires
        {
            foreach (var item in arret.Horaires)
            {
                listBox1.Items.Add(item);
            }
        }
    }

Upvotes: 0

Views: 130

Answers (1)

MikeW
MikeW

Reputation: 1620

Horraire and Destinaire are both annotated to have the XML tag "Horraire" which is confusing the serializer, change the Destinaire class to :

[XmlRoot("root")]
public class Destinataires
{
    [XmlArray("Destinataires")]
    [XmlArrayItem("Destinataire")]
    public ObservableCollection<XML_Arret> Collection { get; set; }
}

Upvotes: 1

Related Questions