Oran
Oran

Reputation: 121

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?

Upvotes: 7

Views: 9127

Answers (4)

Soonts
Soonts

Reputation: 21956

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.

Upvotes: 0

Kluyg
Kluyg

Reputation: 5347

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.

Upvotes: 13

S&#246;ren Kuklau
S&#246;ren Kuklau

Reputation: 19940

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.

Upvotes: 1

Dennis
Dennis

Reputation: 2142

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.

Upvotes: 2

Related Questions