Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal characters in path error while parsing XML in C#

I'm getting an "Illegal characters in path error" while using XMLTextReader method. Basically, I'm sending a long URL to tr.im, and tr.im sends the response as an XML stream, which I'm trying to parse but I get the above mentioned error. Can you guys guide me as to why I'm getting this error and where I'm going wrong? Here's the code:

WebRequest wrURL; Stream objStream; string strURL; wrURL = WebRequest.Create("http://api.tr.im/api/trim_url.xml?url=" + HttpUtility.UrlEncode(txtURL.Text)); objStream = wrURL.GetResponse().GetResponseStream(); StreamReader objSReader = new StreamReader(objStream); strURL = objSReader.ReadToEnd().ToString(); XmlTextReader reader = new XmlTextReader(strURL); //getting the error at this point 

I'm using Visual Studio 2008, Express Edition

like image 752
Sathyajith Bhat Avatar asked Sep 03 '09 17:09

Sathyajith Bhat


People also ask

What does illegal characters in path mean?

The "Illegal characters" exception means that the file path string you are passing to ReadXml is wrong: it is not a valid path. It may contain '?' , or ':' in the wrong place, or '*' for example. You need to look at the value, check what it is, and work out where the illegal character(s) are coming from.

What is XML text reader?

XmlTextReader provides forward-only, read-only access to a stream of XML data. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the read methods and properties reflect the value of the current node.


1 Answers

The reason why is you are using the constructor of XmlTextReader which takes a file path as the parameter but you're passing XML content instead.

Try the following code

XmlTextReader reader = new XmlTextReader(new StringReader(strURL)); 
like image 200
JaredPar Avatar answered Sep 19 '22 12:09

JaredPar