如何反序列化XML - Windows Phone

时间:2014-07-17 12:09:50

标签: c# xml serialization windows-phone

我正在使用此函数反序列化xml文件的数据:

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;

他打电话给Destinataires班:

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

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

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

并调用XML_Arret类:

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

我的XML文件是:

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

但是当我调试我的程序时,我有这样的信息: Une exception de type'System.InvalidOperationException's'est produite dans System.Xml.Serialization.ni.dll mais n'apasétégéréedansle code utilisateur 在线:     XmlSerializer serializer = new XmlSerializer(typeof(Destinataires));

所以,我迷路了,你能帮助我吗?

我为我的语言感到抱歉,我是法国人。

谢谢


我已将代码更改为:

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

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

但我有一个问题:

我使用这个功能:

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

在我的app.xaml.cs中我已经:

 public XML_Arret selectArret { get; set; }

在我的BUS_InfoArret中:     XML_Arret arret;         公共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);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

Horraire和Destinaire都注释了XML标签&#34; Horraire&#34;这使串行器混淆,将Destinaire类更改为:

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