0

I have written the following code in XML and I want to parse it to C# and show it in a ListBox.

This is the code from the XML file:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE root [
  <!ELEMENT root (Persoane*)>
  <!ELEMENT Persoane (nume)>
  <!ELEMENT IsMale (#PCDATA)>
  <!ELEMENT Varsta (#PCDATA)>
  <!ELEMENT PersoanaPlacuta (#PCDATA)>
 ]>
<root>
  <Persoane nume ="Bob">
    <IsMale>true</IsMale>
    <Varsta>30</Varsta>
    <PersoanaPlacuta>"Iulia"</PersoanaPlacuta>
  </Persoane>
</root>

and this is the code in C#:

private void LoadPersoane()
    {
        XmlDocument doc = new XmlDocument();
        XDocument persoanaPlacuta = new XDocument();

        doc.Load("Baza_de_cunostinte.xml");

        foreach (XmlNode node in doc.DocumentElement) 
        {
            string nume = node.Attributes[0].Value;
            int varsta = int.Parse(node["Varsta"].InnerText);
            bool isMale = bool.Parse(node["IsMale"].InnerText);
            persoanaPlacuta = XDocument.Parse(node["PersoanaPlacuta"].InnerText);

            listBox.Items.Add(new Persoane(nume, varsta, isMale, persoanaPlacuta));
        }
    }

Everything works fine, except the field "PersoanaPlacuta". I don't know how to parse it to C#... I tried, but with no luck...

8
  • no luck how? What's it doing wrong? Commented Jan 15, 2016 at 17:30
  • 2
    Get rid of the XDocument.Parse(). Not sure why you added it, but it only causes problems. Commented Jan 15, 2016 at 17:38
  • 1
    node["PersoanaPlacuta"].InnerText is the string "Iulia" why are you attempting to generate an XDocument from it? Commented Jan 15, 2016 at 17:39
  • Why are you mixing the old XmlDocument DOM with the new XDocument DOM? You should really only use one and honestly XDocument is more feature rich. Commented Jan 15, 2016 at 17:50
  • 1
    node["PersoanaPlacuta"].InnerText.Replace("\"", "")? Commented Jan 15, 2016 at 18:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.