Reading Xml from Web Page

Hi, i am trying to read a xml file from the internet and load it in c#, i am getting errors saying "illegal characters in path error" the code i am using is below.

Can anyone help me solve the error

WebRequest myWebRequest = WebRequest.Create("http://www.mattsbox.co.uk/Settings.xml");
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(ReceiveStream,encode);
string strResponse = readStream.ReadToEnd();
readStream.Close();
myWebResponse.Close();

//StreamReader sr = new StreamReader(strResponse.ToString());
XmlTextReader xr = new XmlTextReader(strResponse);
XmlDocument xmlFile = new XmlDocument();
xmlFile.Load(xr);




Answer this question

Reading Xml from Web Page

  • Rob kreger

    The StreamReader expects a filename as a parameter not a string containing the actual data, use e.g. a StringReader instead.

    Replace the last part after myWebResponse.Close() with

    XmlDocument xmlFile = new XmlDocument();
    using (StringReader sr = new StringReader(strResponse))
    {
    using (XmlTextReader xr = new XmlTextReader(sr))
    {
    xmlFile.Load(xr);
    }
    }


  • Richard Morgan

    Is there a readon you can't just say:

    XmlDocument doc=new XmlDocument();
    doc.Load(http://www.site.com/document.xml);


  • Reading Xml from Web Page