2012-02-25 86 views
3

我使用httpwebrequest來獲取一些xml文檔,代碼如下。當我打電話在Windows Phone上使用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) 
       {} 

解析XML文檔。但有時會導致異常,然後我追蹤到發現返回的xml字符串包含「e48」(我使用Fiddler來查找返回的xml字符串),如圖所示。但我找不出原因,這很奇怪,什麼是「e48」?任何人都可以幫忙嗎?

謝謝。

enter image description here

public class HttpGetMethod 
    { 
     public WebCallBack CallBack; 

     public void Request(string url) 
     { 
      var request = (HttpWebRequest)HttpWebRequest.Create(url); 
      IAsyncResult result = null; 

      result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request); 
     } 

     private void ResponseCallback(IAsyncResult result) 
     { 
      try 
      { 
       var request = (HttpWebRequest)result.AsyncState; 
       var response = request.EndGetResponse(result); 

       using (var stream = response.GetResponseStream()) 
       { 
        using (var reader = new StreamReader(stream)) 
        { 

         if (CallBack != null) 
         { 
          var str = reader.ReadToEnd(); 
          CallBack(str); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Deployment.Current.Dispatcher.BeginInvoke(delegate 
       { 
        CallBack(ex.ToString()); 
       }); 
      } 
     } 
    } 

調試輸出是:

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小提琴的原始響應:

enter image description here

+0

這更奇怪的事情是,你打印異常跟蹤顯示無效字符是行897,位置14.我本來期望一個例外如果e48字符是問題,則會出現一條消息,指出無效字符位於數據的最開始處。 – 2012-02-25 06:46:12

+0

如果它在Fiddler的原始響應中顯示,則生成該文件的服務器代碼中存在一個錯誤。你需要調試它。 – 2012-02-25 06:58:51

+0

是啊,看起來像一個服務器的問題,如果你不能訪問服務器,然後只是可能試圖有條件地刪除它,例如if(xml.StartsWith(「e48」)){xml = xml.Substring(3)} – ameer 2012-02-25 18:46:19

回答

0

IMO在這種特定情況下使用正則表達式,如「^ [^ <] *」在

String result = Regex.Replace(htmlDocument, @"^[^<]*", System.String.Empty, RegexOptions.Singleline); 

除去前述垃圾字符

+0

public static string CleanInvalidXmlChars(string text) {0}返回System.Text.RegularExpressions.Regex.Replace(text,@「[^ <] *」,string.Empty); }但它仍然會導致異常。 :-( – ellic 2012-02-25 06:57:33

+1

這些不是垃圾字符 - 它們給出了第一個響應塊的長度 – simonc 2012-02-29 16:49:10

+0

@simonc謝謝,simonc。你說得對,我已經找到了一種處理塊響應的方法,謝謝 – ellic 2012-03-01 04:46:49

相關問題