36

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

5 Answers 5

78

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));
Sign up to request clarification or add additional context in comments.

2 Comments

Amazing. Thanks, your suggestion did not result in the error.
Won't work unless you change the request to receive xml instead of the default pretty HTML setting. Add this parameter to your request: format=xml
16

XmlTextReader constructor accepts a string that points to the URL where an XML file is stored. You are passing it the XML itself which of course is an invalid path. Try this instead:

using (var client = new WebClient())
{
    var xml = client.DownloadString("http://api.tr.im/api/trim_url.xml?url=" + HttpUtility.UrlEncode(txtURL.Text));
    using (var strReader = new StringReader(xml))
    using (var reader = XmlReader.Create(strReader))
    {

    }
}

Comments

5

The XmlTextReader(string) constructor expects a file path, not the actual XML data.

You can create an XML reader directly from the stream. The recommended way to do this is using the XmlReader.Create method:

XmlReader reader = XmlReader.Create(objStream);

2 Comments

getting Error: 'System.Xml.XmlReader.Create(string)' is a 'method' but is used like a 'type' with this.
Edited to take out "new", which was the problem.
1

You should print or otherwise display strUrl. Once you can actually see the path that you're passing to the test reader, it should be obvious what the path error is.

Also, just looking at the code, it seems like the response itself might be XML, in which case you should pass objSReader directly to the XmlTextReader constructor.

1 Comment

The response is XML data, as mentioned in the Question. Even with just passing objSReader I was getting the error.
1
private void csv2_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        DataSet dsSchema = new DataSet();
        dsSchema.ReadXml(@"C:\Working\Teradata\ssis\Sample.xml");
        StringReader sreader = new StringReader(ToXml(dsSchema));
         ds.ReadXmlSchema(sreader);
         ds.ReadXml(@"C:\Working\Teradata\ssis\Sample.xml");
        ExportTableToCsvString(ds.Tables["session"], true, @"C:\Working\Teradata\ssis\op\session.csv");
        BuildDynamicTable(ds, @"C:\Working\Teradata\ssis\op\");

    }
    public string ToXml(DataSet ds)
    {
        using (var memoryStream = new MemoryStream())
        {
            using
                   (
                   TextWriter streamWriter = new StreamWriter(memoryStream))
            {
                var xmlSerializer = new XmlSerializer(typeof(DataSet));
                xmlSerializer.Serialize(streamWriter, ds);
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
        }
    }

1 Comment

what am I supposed to do with this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.