【问题标题】:Is XDocument.Parse different in Windows Phone 7?Windows Phone 7 中的 XDocument.Parse 是否不同?
【发布时间】:2011-05-03 05:21:53
【问题描述】:

我在 WP7 应用程序和 C# 3.5 应用程序中运行完全相同的代码。 WP7 应用程序在调用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);
}

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

编辑:

异常消息是“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)

编辑2:

我已经完成了建议的操作,并查看了 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

【问题讨论】:

  • 请发布完整的堆栈跟踪,包括异常的完整消息。
  • 请注意,一些服务器会根据用户代理更改输出 - 在本例中是移动浏览器。
  • @Jon 添加了堆栈跟踪。 @BuildStarted 嗯,好主意。我尝试比较从 2 个请求输出的字符串,一眼看去,这两个字符串中似乎存在一些编码差异。我会尝试更改用户代理,看看是否有任何不同。
  • @johnhforrest:正如 BuildStarted 提到的,数据可能不同。可能值得查看调试器中的文本并发布前几行(看起来是 doctype 声明在这里引起了问题)。
  • 好的,只是花了一些时间比较基于用户代理的输出,在这种情况下似乎没有什么区别。在 WP7 模拟器中执行请求时,即使使用相同的用户代理,3.5 应用程序端也会显示奇怪的字符。

标签: c# silverlight windows-phone-7


【解决方案1】:

问题在于 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
}

【讨论】:

  • 第一个示例确实解决了解析问题(在将 e.Result 包装在 MemoryStream 中以解决编译错误之后),但我无法在 WP7 环境中构建第二个示例,我没有'认为他们不支持 XmlPreloadedResolver。知道 Silverlight 应用程序不喜欢 DTD 的原因吗?
  • 实际上看起来所有信息都在这里推理:msdn.microsoft.com/en-us/library/cc189013(v=vs.95).aspx
  • @johnhforrest:您需要引用 System.Xml.Utils dll 来获取 XmlPreloadedResolver。我会尽快调整答案以解决错误,我不会使用 MemoryStream,而是使用StringReader。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2012-07-30
  • 1970-01-01
相关资源
最近更新 更多