尝试解析plist文档时出现NotSupportedException

mwi*_*ann 0 xml linq-to-xml windows-phone

我正在为我工​​作的公司开发Windows Phone 7应用程序.对于配置部分,我想分享用于我们的iPhone应用程序并存储在plist文件中的远程服务器上的配置.

我使用System.Xml.Linq.XDocumentParse是使用WebClient实例下载的字符串.

这是代码:

        Uri plistLocation = new 
            Uri(@"http://iphonevnreporter.vol.at/Settings.bundle/mw_test.plist");
        WebClient client = new WebClient();

        try
        {
            client.DownloadStringCompleted += ((sender,e) => {
                if (e.Error == null)
                {
                    XDocument xdoc = XDocument.Parse(e.Result);

                    //XElement element = XElement.Parse(e.Result.ToString());
                    var dictItems = xdoc.Descendants("dict");
                    foreach (XElement elem in dictItems)
                    {
                    }
                }
            });
        }
        catch (Exception e)
        {
        }
        client.DownloadStringAsync(plistLocation);
Run Code Online (Sandbox Code Playgroud)

在这个例子中,plist只是dict在根元素下面有一个元素plist,然而我正在接收NotSupportedException.异常发生在XDocument.Parse(e.Result).

这是StackTrace:

   at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.Linq.XDeclaration..ctor(XmlReader r)
   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 VorarlbergOnline.MainViewModel.<FillSections>b__10(
                 Object sender, DownloadStringCompletedEventArgs e)
   at System.Net.WebClient.OnDownloadStringCompleted
            (DownloadStringCompletedEventArgs     e)
   at System.Net.WebClient.DownloadStringOperationCompleted(Object arg)
   at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext 
        executionContext,     ContextCallback callback, Object state)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
Run Code Online (Sandbox Code Playgroud)

加载其他XML文件工作正常,所以代码似乎没问题.我检查引用的dtd是否可能是问题,但它加载正常.所以我现在有点想法了.

Jon*_*eet 5

好的,现在我实际上是在原始文件而不是浏览器中查看文件,我确定这是问题所在:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
          "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Run Code Online (Sandbox Code Playgroud)

看起来像Windows Phone 7中不支持doctype解析.你可以做一个快速而肮脏的黑客来删除它:

string xml = e.Result;
int docTypeIndex = xml.IndexOf("<!DOCTYPE");
if (docTypeIndex != -1)
{
    int docTypeEnd = xml.IndexOf(">", docTypeIndex);
    // TODO: Decide what to do if docTypeEnd is -1...
    xml = xml.Substring(0, docTypeIndex) + xml.Substring(docTypeEnd + 1);
}
Run Code Online (Sandbox Code Playgroud)