【发布时间】: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