NotSupportedException when trying to parse plist document
I'm currently working on a Windows Phone 7 Application for the company I work for. For the configuration part I'd like to share the configuration that is used for our iPhone Application and stored on a remote server in a plist file.
I use System.Xml.Linq.XDocument
to Parse
a string I downloaded using a WebClient instance.
This is the code:
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);
In this example the plist is just having a dict
Element under the root plist
element and nevertheless I am receiving the NotSupportedException
. The Exception occurs at XDocument.Parse(e.Result)
.
This is the Stack开发者_运维技巧Trace:
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()
Loading other XML files works fine, so the code seems to be ok. I checked if the referenced dtd could be the problem, but it loads fine. So I'm kind of out of ideas now.
Okay, now I've actually looked at the file in the raw rather than via a browser, I'm sure this is the problem:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
It looks like doctype parsing just isn't supported in Windows Phone 7. You could do a quick and dirty hack to remove it though:
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);
}
The problem really is the DOCTYPE, that cannot be parsed by XDocument
on Windows Phone. A shorter solution would be using Regex to remove the DOCTYPE reference:
string replaced = Regex.Replace(e.Result, "<!DOCTYPE.+?>", string.Empty);
XDocument.Parse(replaced);
精彩评论