34

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

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134

5 Answers5

76

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));
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
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))
    {

    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

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);
JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
dtb
  • 213,145
  • 36
  • 401
  • 431
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());
            }
        }
    }
JAY
  • 81
  • 1
  • 2
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.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169