简体   繁体   中英

Parsing .plist Files to plain XML C#

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?

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM