Search code examples
c#xmlxelement

Xelement tags issue


enter code herehey guys im having trouble with xelement when im trying to open the test page an unhandled exception appears and that because the


tag doesn't match the closing in another line i tried to add the closings tag but the error happens before the adding function could work

test page http://densetsu.org/PP2012/benchmark1.html

so is there is a way to pass the tag problems without losing the tag effect
this is the main code:

             XElement tree = XElement.Load(toolStripTextBox1.Text);  
             String s = tree.ToString();  
             textBox1.Text = String_dealer.addmissing(s);

this is the string changer

        public static String addmissing(String txt)
    {
        if (txt.Contains("<br>") || (txt.Contains("</br>")))
        {
            txt.Replace("<br>", "<br></br>");
            txt.Replace("</br>", "<br></br>");
        }
        else if (txt.Contains("<hr>") || (txt.Contains("</hr>")))
        {
            txt.Replace("<hr>", "<hr> </hr>");
            txt.Replace("</hr>", "<hr> </hr>");
        }

        return txt;
    }

and the problem text : An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

Additional information: The 'hr' start tag on line 8 does not match the end tag of 'br'. Line 9, position 10.


Solution

  • using a stream reader from a web request then storing the stream into an String file after that you can pass the string to sgml.reader which will transform the html into valid xml