2010-03-28 14 views
5

Sto provando ad analizzare questo xml, ma C# continua a lanciare un'eccezione dicendo che ha caratteri non validi. Non riesco a copiare il testo direttamente dalla messagebox, quindi l'ho sottoposto a screening.Edizione XmlTextReader

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

Edit: il testo copiato

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

Ecco il codice per ottenere la stringa

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

È possibile premere Ctrl + C per copiare il testo di un MessageBox standard. – SLaks

+0

Grazie per il suggerimento. Ora l'ho testato con HexEdit e pulito:/ –

+3

stai usando .net 3.5? In tal caso, perché non usare LINQ in XML? Fare manipolazioni di stringhe su XML è puro masochismo! :) –

risposta

9

XmlTextReader si aspetta un URL contenente il codice XML e non la stessa XML come stringa. Per analizzare il codice XML con un XmlTextReader è necessario creare un ruscello e la fornitura al XmlTextReader

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

Ah, sono stato espulso perché il doc msdn ha detto che XmlTextReader è stato sovraccaricato per accettare una stringa. –