Illegal characters in path error while parsing XML in C#

会有一股神秘感。 提交于 2019-12-18 05:27:10

问题


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


回答1:


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));



回答2:


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))
    {

    }
}



回答3:


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);



回答4:


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.




回答5:


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());
            }
        }
    }


来源:https://stackoverflow.com/questions/1374729/illegal-characters-in-path-error-while-parsing-xml-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!