开发者

Is XDocument.Parse different in Windows Phone 7?

开发者 https://www.devze.com 2023-03-02 22:55 出处:网络
I am running the exact same code in a WP7 application and a C# 3.5 application. The WP7 application throws a NotSupportedException upon calling XDocument.Parse() while the C# 3.5 application parses th

I am running the exact same code in a WP7 application and a C# 3.5 application. The WP7 application throws a NotSupportedException upon calling XDocument.Parse() while the C# 3.5 application parses the XML with no problems. Below is the code used:

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);
}

Any idea why this is happening? It's strange that it is failing when trying to parse an SC2 forum when a WoW forum works just fine (http://us.battle.net/wow/en/forum/984270/).

Edit:

The exception message is "NotSupportedException". Here's the full stack trace:

   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.Wind开发者_开发技巧ows.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:

I've done what was suggested and have looked at the output of the 2 different requests. Additionally in my 3.5 client application I forced the user-agent to be the same as it is in the WP7 emulator to ensure it's not the user-agent causing the issue.

Here's the doctype declaration copied from Visual Studio:

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

The doctypes are the same in each document but there are a few discrepancies in the files that stand out (it looks like there are a few extra characters inserted on the 3.5 side):

WP7 Emulator: StarCraft II

3.5 Application: StarCraft II


The problem is that XDocument.Parse enables DTD processing (which is normally disabled by default on a XmlTextReader) yet it does not supply a resolver. Try this code instead:

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
}

OR:-

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消