开发者

XmlTextReader issue

开发者 https://www.devze.com 2022-12-24 04:15 出处:网络
I\'m try to parse this xml, but c# keeps throwing an exception saying it has invalid characters. I can\'t copy the text from the messagebox directly, so I\'ve screened it.

I'm try to parse this xml, but c# keeps throwing an exception saying it has invalid characters. I can't copy the text from the messagebox directly, so I've screened it.

http://img29.imageshack.us/img29/694/xmler.jpg

Edit: copied text

<?xml version="1.0" encoding="UTF-8"?><user><id>9572</id><screen_name>fgfdgfdgfdgffg44</screen_name></user>

Here's the code to get the string

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


The XmlTextReader is expecting an url containing the XML and not the XML itself as a string. To parse the XML with a XmlTextReader you must create a stream and supply it to the XmlTextReader

using (StringReader stringReader = new StringReader(servReply))
{
    using (XmlTextReader xmlTextReader = new XmlTextReader(stringReader))
    {
        // Read the xml
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消