在 C# 中解析 XML 时路径错误中的非法字符 [英] Illegal characters in path error while parsing XML in C#

查看:103
本文介绍了在 C# 中解析 XML 时路径错误中的非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XMLTextReader 方法时收到路径中的非法字符错误".基本上,我向 tr.im 发送了一个长 URL,而 tr.im 将响应作为 XML 流发送,我正在尝试解析它,但出现上述错误.你们能指导我为什么我会收到这个错误以及我哪里出错了吗?代码如下:

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

我使用的是 Visual Studio 2008 速成版

I'm using Visual Studio 2008, Express Edition

推荐答案

原因是您使用 XmlTextReader 的构造函数,该构造函数将文件路径作为参数,但您传递的是 XML 内容.

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 reader = new XmlTextReader(new StringReader(strURL));

这篇关于在 C# 中解析 XML 时路径错误中的非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆