0

I am trying to read XML Value from a XML String. below is my Code: I want to read the value of the title tag: "503 Service Unavailable" Below XML is the response posted by Server -

 string xmldata = "<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>503 Service Unavailable</title>
    </head><body>
    <h1>Service Unavailable</h1>
    <p>The server is temporarily unable to service your
    request due to maintenance downtime or capacity
    problems. Please try again later.</p>
    </body></html>";

Code:

   var rootElement = XElement.Parse(xmldata);
   var errorTitle = rootElement.Element("title").Value;

I have tried this code as well :

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.LoadXml(xmldata);
 string xPath = "html/head";
 var nodes = xmlDoc.SelectNodes(xPath);
 foreach (XmlNode childrenNode in nodes)
 {
    string item = childrenNode.InnerText;
 }

Both pieces of code give me an error as - '>' is an unexpected token. The expected token is '"' or '''. Line 1, position 50.

Please suggest.

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
Bokambo
  • 4,204
  • 27
  • 79
  • 130
  • Are there any other options instead of including HtmlAgilityPack ? – Bokambo Oct 21 '20 at 20:23
  • 1
    Yes. Check other questions like https://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c – Progman Oct 21 '20 at 20:25
  • the DOCTYPE notation is invalid according to xml syntax validators. it is missing a second part. if you change your example to ``` ``` the string can be parsed by xDocument. – martijn Oct 22 '20 at 05:45

0 Answers0