在Windows phone中使用HttpWebRequest获取xml

在Windows phone中使用HttpWebRequest获取xml,xml,windows-phone-7,httpwebrequest,Xml,Windows Phone 7,Httpwebrequest,我使用httpwebrequest获取一些xml文档,代码如下。当我打电话时 HttpGetMethod http = new HttpGetMethod(); http.Request("http://sample.com/xml.php"); 它工作正常,然后我使用 XDocument document = XDocument.Parse(xml); XElement element = document.Element("statuses");

我使用httpwebrequest获取一些xml文档,代码如下。当我打电话时

HttpGetMethod http = new HttpGetMethod();
http.Request("http://sample.com/xml.php");
它工作正常,然后我使用

XDocument document = XDocument.Parse(xml);

                XElement element = document.Element("statuses");
                IEnumerable<XElement> statusesElements = element.Elements("status");

                foreach (var elx in statusesElements)
                {}
调试输出为:

A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
System.Xml.XmlException: '', hexadecimal value 0x0C, is an invalid character. Line 897, position 14.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
   at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, Int32 res, String resString, String[] args)
   at System.Xml.XmlTextReaderImpl.ThrowInvalidChar(Char[] data, Int32 length, Int32 invCharPos)
   at System.Xml.XmlTextReaderImpl.ParseCDataOrComment(XmlNodeType type, Int32& outStartPos, Int32& outEndPos)
   at System.Xml.XmlTextReaderImpl.ParseCDataOrComment(XmlNodeType type)
   at System.Xml.XmlTextReaderImpl.ParseCData()
   at System.Xml.XmlTextReaderImpl.Parse
以下是ih Fiddle的原始响应:


IMO在此特定场景中使用regex,例如“^[^更奇怪的是,您打印的异常跟踪显示无效字符位于第897行的第14位。如果e48字符是问题所在,我会期望出现异常,并显示一条消息,说明无效字符位于数据的最开头。如果Fiddler生成文件的服务器代码中有一个错误。您需要调试它。是的,看起来像是服务器问题,如果您无法访问服务器,那么可以尝试有条件地删除它。例如,如果(xml.StartsWith(“e48”){xml=xml.Substring(3)}来自服务器的响应被分块(请参阅传输编码头以获得确认)。e48行给出了第一个数据块中的字节数。响应是否结束\r\n0\r\n?如果结束,您的服务器可能运行正常。再仔细考虑一下,尝试将响应流复制到内存流,从该流中提取字节,转换为utf8是值得的(或响应给出的任何其他编码)然后将其馈送到xml解析器中。公共静态字符串CleanInvalidXmlChars(字符串文本){return System.text.RegularExpressions.Regex.Replace(text,@“[^这些不是垃圾字符-它们给出了第一个响应块的长度。@simonc谢谢,simonc。你说得对,我找到了一种处理响应块的方法,非常感谢。@ellic你的解决方案是什么?你能在这里发布吗。
A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
System.Xml.XmlException: '', hexadecimal value 0x0C, is an invalid character. Line 897, position 14.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
   at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, Int32 res, String resString, String[] args)
   at System.Xml.XmlTextReaderImpl.ThrowInvalidChar(Char[] data, Int32 length, Int32 invCharPos)
   at System.Xml.XmlTextReaderImpl.ParseCDataOrComment(XmlNodeType type, Int32& outStartPos, Int32& outEndPos)
   at System.Xml.XmlTextReaderImpl.ParseCDataOrComment(XmlNodeType type)
   at System.Xml.XmlTextReaderImpl.ParseCData()
   at System.Xml.XmlTextReaderImpl.Parse
String result = Regex.Replace(htmlDocument, @"^[^<]*", System.String.Empty, RegexOptions.Singleline);