Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlTextReader issue

Tags:

c#

xml

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);
like image 210
stan Avatar asked Mar 28 '10 01:03

stan


1 Answers

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
    }
}
like image 188
Alexandre Pepin Avatar answered Oct 16 '22 01:10

Alexandre Pepin