Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not read XML document containing ampersand symbol

Tags:

c#

xml

I am writing a program that reads a XML file with Visual C#. I have a problem reading the Xml file, because it contains invalid XML symbols, for example '&'.

I have to read the XML but I can not modify the document. How can I modify the Xml file using C#? My code so far:

    private void button1_Click(object sender, EventArgs e)
    {
        XmlDocument doc;
        doc = new XmlDocument();
        doc.Load("nuevo.xml");


        XmlNodeList Xpersonas = doc.GetElementsByTagName("personas");
        XmlNodeList Xlista = ((XmlElement)Xpersonas[0]).GetElementsByTagName("edad");

            foreach (XmlElement nodo in Xlista)
            {
                string edad = nodo.GetAttribute("edad");
                string nombre = nodo.InnerText;
                textBox1.Text = nodo.InnerXml;
            }
like image 859
garci Avatar asked Oct 19 '22 11:10

garci


1 Answers

As @EBrown suggested, one possibility would be read the file content in a string variable and replace the & symbol with the correct representation for propert XML & and then parse the XML structure. A possible solution could look like this:

var xmlContent = File.ReadAllText(@"nuevo.xml");
XmlDocument doc;
doc = new XmlDocument();
doc.LoadXml(xmlContent.Replace("&", "&"));

XmlNodeList Xpersonas = doc.GetElementsByTagName("personas");
XmlNodeList Xlista = ((XmlElement)Xpersonas[0]).GetElementsByTagName("edad");

foreach (XmlElement nodo in Xlista)
{
    string edad = nodo.GetAttribute("edad");
    string nombre = nodo.InnerText;
    Console.WriteLine(nodo.InnerXml.Replace("&", "&"));
}

The output is:

34 & 34 

If it is ok to use LINQ2XML, then the solution is even shorter, and there is no need to write the reverse(second) replace, because LINQ2XML make this for you automatically:

var xmlContent = File.ReadAllText(@"nuevo.xml");
var xmlDocument = XDocument.Parse(xmlContent.Replace("&", "&"));
var edad = xmlDocument.Root.Element("edad").Value;
Console.WriteLine(edad);

The output is the same as above.

like image 114
keenthinker Avatar answered Oct 22 '22 02:10

keenthinker