我在 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);
}
知道为什么会这样吗?奇怪的是,在 WoW 论坛正常工作时尝试解析 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 html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
每个文档中的文档类型都相同,但文件中存在一些突出的差异(看起来在 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
}
关于c# - Windows Phone 7 中的 XDocument.Parse 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5865374/