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:
在使用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
I'm using Visual Studio 2008, Express Edition
我正在使用Visual Studio 2008, Express Edition
5 个解决方案
#1
67
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.
原因是您正在使用XmlTextReader的构造函数,该构造函数以文件路径作为参数,但您正在传递XML内容。
Try the following code
试试下面的代码
XmlTextReader reader = new XmlTextReader(new StringReader(strURL));
#2
12
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:
XmlTextReader构造函数接受指向存储XML文件的URL的字符串。您正在将XML本身传递给它,这当然是无效的路径。试试这个:
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
4
The XmlTextReader(string)
constructor expects a file path, not the actual XML data.
XmlTextReader(string)构造函数期望的是文件路径,而不是实际的XML数据。
You can create an XML reader directly from the stream. The recommended way to do this is using the XmlReader.Create method:
您可以直接从流中创建XML阅读器。推荐的方法是使用XmlReader。创建方法:
XmlReader reader = XmlReader.Create(objStream);
#4
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.
您应该打印或显示strUrl。一旦您实际看到了要传递给测试阅读器的路径,路径错误应该是显而易见的。
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.
另外,只需查看代码,就会发现响应本身可能是XML,在这种情况下,应该将objSReader直接传递给XmlTextReader构造函数。
#5
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
67
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.
原因是您正在使用XmlTextReader的构造函数,该构造函数以文件路径作为参数,但您正在传递XML内容。
Try the following code
试试下面的代码
XmlTextReader reader = new XmlTextReader(new StringReader(strURL));
#2
12
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:
XmlTextReader构造函数接受指向存储XML文件的URL的字符串。您正在将XML本身传递给它,这当然是无效的路径。试试这个:
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
4
The XmlTextReader(string)
constructor expects a file path, not the actual XML data.
XmlTextReader(string)构造函数期望的是文件路径,而不是实际的XML数据。
You can create an XML reader directly from the stream. The recommended way to do this is using the XmlReader.Create method:
您可以直接从流中创建XML阅读器。推荐的方法是使用XmlReader。创建方法:
XmlReader reader = XmlReader.Create(objStream);
#4
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.
您应该打印或显示strUrl。一旦您实际看到了要传递给测试阅读器的路径,路径错误应该是显而易见的。
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.
另外,只需查看代码,就会发现响应本身可能是XML,在这种情况下,应该将objSReader直接传递给XmlTextReader构造函数。
#5
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());
}
}
}