Parsing XML data in C# and show into ListBox -
i trying parse xml file in c# using visual studio , show data in listbox, don't know how parse when i'm dealing nested xml file.
this code xml file:
<?xml version="1.0" encoding="utf-8" ?> <!doctype root [ <!element root (persons*)> <!element persons (name)> <!element ismale (#pcdata)> <!element age (#pcdata)> <!element name (#pcdata)> <!element likedperson (name)> ]> <root> <persons name ="bob"> <ismale>true</ismale> <age>30</age> <likedperson name ="iulia"> <ismale>false</ismale> <age>32</age> </likedperson> </persons> </root>
the code i've written in c# return me name, gender , age every person, don't know how write show me person_liked:
private void loadpersons() { xmldocument doc = new xmldocument(); doc.load("baza_de_cunostinte.xml"); foreach (xmlnode node in doc.documentelement) { string name = node.attributes[0].value; int age = int.parse(node["age"].innertext); bool ismale = bool.parse(node["ismale"].innertext); // persons likedperson.name = persons.node.attributes[0].value ? // ..... listbox.items.add(new persons(name, age, ismale, likedperson)); } } private void listbox_selectedindexchanged(object sender, eventargs e) { if (listbox.selectedindex != -1) { propertygrid1.selectedobject = listbox.selecteditem; } }
this definition of persons.cs :
class persons { public string name { get; private set; } public int age { get; private set; } public bool ismale { get; private set; } public persons likedperson { get; private set; } public persons(string name, int age, bool ismale, persons likedperson) { name = name; age = age; ismale = ismale; likedperson = likedperson; } }
xmlserializer myserializer = new xmlserializer(typeof(persons)); // create filestream or textreader read xml data. filestream myfilestream = new filestream("xmldatafile.xml", filemode.open); var person = (persons) myserializer.deserialize(myfilestream);
you need add constructor without parameter persons class.
Comments
Post a Comment