我在使用 XMLTextReader 方法时收到“路径错误中的非法字符”。基本上,我向 tr.im 发送了一个长 URL,tr.im 将响应作为 XML 流发送,我试图对其进行解析,但出现了上述错误。你们能指导我为什么会收到此错误以及我哪里出错了吗?这是代码:
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
我使用的是 Visual Studio 2008 速成版
最佳答案
原因是您使用 XmlTextReader 的构造函数,该构造函数将文件路径作为参数,但您传递的是 XML 内容。
试试下面的代码
XmlTextReader reader = new XmlTextReader(new StringReader(strURL));
关于c# - 在 C# 中解析 XML 时出现路径错误中的非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1374729/