我试图解析这个xml,但是c#不断抛出一个异常,说它有无效的字符。我无法直接从邮箱中复制文本,因此我已对其进行了筛选。
http://img29.imageshack.us/img29/694/xmler.jpg
编辑:复制文字
<?xml version="1.0" encoding="UTF-8"?><user><id>9572</id><screen_name>fgfdgfdgfdgffg44</screen_name></user>
这是获取字符串
的代码string strRetPage = System.Text.Encoding.GetEncoding(1251).GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = socket.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + System.Text.Encoding.GetEncoding(1251).GetString(RecvBytes, 0, bytes);
}
start = strRetPage.IndexOf("<?xml");
string servReply = strRetPage.Substring(start);
servReply = servReply.Trim();
servReply = servReply.Replace("\r", "");
servReply = servReply.Replace("\n", "");
servReply = servReply.Replace("\t", "");
XmlTextReader txtRdr = new XmlTextReader(servReply);
答案 0 :(得分:9)
XmlTextReader期望包含XML的URL而不是XML本身作为字符串。要使用XmlTextReader解析XML,您必须创建一个流并将其提供给XmlTextReader
using (StringReader stringReader = new StringReader(servReply))
{
using (XmlTextReader xmlTextReader = new XmlTextReader(stringReader))
{
// Read the xml
}
}