我正在尝试将XHTML文档加载到XDocument中,但我得到了"引用未声明的实体"的例外情况.我需要解决像®
和的实体»
.
我相信我的文件是正确形成的,这是头部:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
Run Code Online (Sandbox Code Playgroud)
当我这样做XDocument.Load(<StringReader>)
时,我会抛出这些异常.
我在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 …
Run Code Online (Sandbox Code Playgroud)