【问题标题】:How can I deserialize an XML - Windows Phone如何反序列化 XML - Windows Phone
【发布时间】:2014-09-08 07:16:51
【问题描述】:

我正在使用这个函数来反序列化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>

但是当我调试我的程序时,我收到了这条消息: 在 System.Xml.Serialization.ni.dll 中使用 'System.InvalidOperationException' 类型的一个异常可能是不可用的代码实用程序 在线: XmlSerializer 序列化器 = 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() { 初始化组件();

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

【问题讨论】:

  • 抱歉,我在那里不小心按了返回,请看我的回答,而不是没有意义的评论。

标签: c# xml serialization windows-phone


【解决方案1】:

Horraire 和 Destinaire 都被注释为具有混淆序列化程序的 XML 标签“Horraire”,将 Destinaire 类更改为:

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

【讨论】:

  • 感谢它的工作,但我想在我的列表中选择一个元素,例如“Faubourd d'isle”,查看她的日程安排。
  • 当可以正确读取 XML 时,所有数据都应该存在。那么您的问题是如何显示该数据?无论如何,当这个答案解决了你的问题时,你应该接受它而不是用不同的问题来扩展它:)
  • 我已经在 MikeW 的帮助下更改了我的初始答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多