Parsing .plist Files to plain XML C#

2020-07-10 08:02发布

问题:

I'm trying to read my Apple Safari history with c#, which is stored in a plist file, however I always get an error and I'm not sure what the correct way is to do it. The code I tried to execute is this:

XmlDocument xmd = new XmlDocument();
xmd.LoadXml(@"C:\Users\Oran\AppData\Roaming\AppleComputer\Safari\History.plist");

and I always get the following error: "Data at the root level is invalid. Line 1, position 1."

Does anyone know whats wrong with this code and recommend what is the best way to read plist files?

回答1:

It looks like that Apple Safari history.plist is binary plist. I've found a great project:

https://github.com/animetrics/PlistCS

From the readme:

This is a C# Property List (plist) serialization library (MIT license). It supports both XML and binary versions of the plist format.



回答2:

try this and everyhing should be fine ;-)

xmd.Load(...)

The one you have used loads the xml data from a string not from a file.



回答3:

A plist doesn't have to be XML. There are four different serialization methods — old-style (for NeXT; no longer used), XML, binary and JSON (new in 10.7). Safari's History.plist is most likely binary, for efficiency reasons.

If I'm not mistaken, Safari for Windows does ship with plutil.exe in Common Files\Apple Application Support. You can use that like plutil -convert xml1 SOME_FILE.plist to convert your file.



回答4:

The problem is with the second line, saying

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  • Option 1. Remove it before parsing.
  • Option 2. Read the MSDN on "XmlDocument.XmlResolver Property" and figure out how to make the XmlDocument download, parse and use the DTD from the URI specified in the XML.


标签: c# plist