I'm using this function to deserialise the data of the xml file:
He call the Destinataires class:
- 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;
- [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"?>
Faubourd d'isle images/1.png 6h18 6h28 6h38 - ...
- ...
</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.dllmais n'a pas été gérée dans le code utilisateurà la ligne: 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
VulpesPosted Jul 18, 2014, 7:33 AM
I think the problem here is that your XML attributes are not quite.
When I changed them to the following, your file deserialized correctly:
VulpesPosted Jul 18, 2014, 7:31 PM
Valentin MartinPosted Jul 18, 2014, 11:24 AM
Thanks for your help !
Ramesh MaruthiPosted Jul 18, 2014, 11:13 AM
Try the Vulpes code, its working even for me :)
Thanks Vulpes sir :)
Valentin MartinPosted Jul 17, 2014, 4:30 PM
I've try with your changement:
And for display Destinataire. Designation in my ListBox:
listBox1.ItemsSource = XmlData.Destinataire; ???
And:
But I've a problem: nothing appears on my screen. So I think I've a problem when I sent my data to the ListBox, or is in the DestinatairesList or Destinataire.
Ramesh MaruthiPosted Jul 17, 2014, 11:16 AM
...
...
[XmlRoot("Destinataires")]
public class DestinatairesList
{
[XmlElement("Destinataire")]
public List
}
public class Destinatarie
{
[XmlElement("Designation")]
public string Designation { get; set; }
[XmlElement("Carre")]
public string Carre { get; set; }
[XmlElement("Horraires")]
public List
}
public class Horraire
{
[XmlElement("Horraire")]
public string Horraire { get; set; }
}
XmlSerializer deserializer = new XmlSerializer(typeof(DestinatarieDestinatarie));
TextReader reader = new StreamReader(@"D:\myXml.xml");
object obj = deserializer.Deserialize(reader);
Destinatarie XmlData = (Destinatarie)obj;
reader.Close();
Valentin MartinPosted Jul 17, 2014, 10:54 AM
Ramesh MaruthiPosted Jul 17, 2014, 10:39 AM
Now I understood your problem you want do deserialize even the child nodes right ?
can you give try in this way
Ramesh MaruthiPosted Jul 17, 2014, 10:27 AM
Did u write this lines in car class ?
Valentin MartinPosted Jul 17, 2014, 10:07 AM
My program doesn't work. I don't arrive to deserialise the Technical Control level
Ramesh MaruthiPosted Jul 17, 2014, 9:59 AM
Can you try it in this way