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

joh*_*est 3 c# silverlight windows-phone-7

我在WP7应用程序和C#3.5应用程序中运行完全相同的代码.在WP7应用程序将引发NotSupportedException在调用XDocument.Parse(),而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);
}
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?奇怪的是,当一个魔兽世界论坛工作得很好时(http://us.battle.net/wow/en/forum/984270/)尝试解析SC2论坛时失败了.

编辑:

异常消息是"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)
Run Code Online (Sandbox Code Playgroud)

EDIT2:

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

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

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">

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

WP7模拟器: StarCraft II

3.5申请: StarCraft II

Ant*_*nes 8

问题是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
}
Run Code Online (Sandbox Code Playgroud)

要么:-

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
}
Run Code Online (Sandbox Code Playgroud)