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...
node["PersoanaPlacuta"].InnerTextis the string"Iulia"why are you attempting to generate an XDocument from it?XmlDocumentDOM with the newXDocumentDOM? You should really only use one and honestlyXDocumentis more feature rich.node["PersoanaPlacuta"].InnerText.Replace("\"", "")?