Windows Phone 7中的XDocument.Parse是否有所不同?

我在WP7应用程序和C#3.5应用程序中运行完全相同的代码。 XDocument.Parse() #应用程序在调用XDocument.Parse()时抛出NotSupportedException ,而C#3.5应用程序解析XML没有问题。 以下是使用的代码:

 WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadThreadsComplete); client.DownloadStringAsync(new Uri("http://us.battle.net/sc2/en/forum/40568/", UriKind.Absolute)); ... private static void DownloadThreadsComplete(object sender, DownloadStringCompletedEventArgs e) { var doc = XDocument.Parse(e.Result); } 

知道为什么会这样吗? 奇怪的是,当一个WoW论坛工作正常(http://us.battle.net/wow/en/forum/984270/)时,它试图解析SC2论坛时失败了。

编辑:

exception消息是“NotSupportedException”。 这是完整的堆栈跟踪:

  at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options) at System.Xml.Linq.XDocument.Parse(String text) at SC2ForumReader.Pages.ForumViewerPage.DownloadThreadsComplete(Object sender, DownloadStringCompletedEventArgs e) at System.Net.WebClient.OnDownloadStringCompleted(DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Delegate.DynamicInvokeOne(Object[] args) at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) at System.Delegate.DynamicInvoke(Object[] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) at System.Windows.Threading.Dispatcher.OnInvoke(Object context) at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult) 

EDIT2:

我已经完成了建议,并查看了2个不同请求的输出。 此外,在我的3.5客户端应用程序中,我强制用户代理与WP7仿真器中的用户代理相同,以确保它不是导致问题的用户代理。

这是从Visual Studio复制的doctype声明:

每个文档中的doctypes都是相同的,但是文件中有一些差异很突出(看起来在3.5方面插入了一些额外的字符):

WP7模拟器: StarCraft II

3.5应用: StarCraft II

问题是XDocument.Parse启用DTD处理(默认情况下通常在XmlTextReader上禁用)但它不提供解析器。 请尝试使用此代码:

 private static void DownloadThreadsComplete(object sender, DownloadStringCompletedEventArgs e) { XDocument doc; XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader reader = XmlReader.Create(new StringReader(e.Result), settings)) { doc = XDocument.Load(reader); } // Do stuff with doc } 

要么:-

 private static void DownloadThreadsComplete(object sender, DownloadStringCompletedEventArgs e) { XDocument doc; XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Parse; settings.XmlResolver = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10); using (XmlReader reader = XmlReader.Create(new StringReader(e.Result), settings)) { doc = XDocument.Load(reader); } // Do stuff with doc }