Parsing .plist Files to plain XML C#
Asked Answered
M

4

7

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?

Marksman answered 25/10, 2010 at 12:34 Comment(0)
A
13

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.

Atrophied answered 9/2, 2012 at 8:27 Comment(1)
Great project! Included it in no time.Latonya
F
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.

Frankenstein answered 25/10, 2010 at 13:13 Comment(4)
it still gives me the same error... maybe apple serialze safari's plists somehow?Marksman
1.) Can you confirm, that the xml is well-formed? Try to open it with an xml editor. 2.) Make sure that the file is there. 3.) Make sure there is no blank line before the root element 4.) What about security? Has you app the privilege to open the file? (Maybe when your app is a web-service or website it doesn't have the privilege)Frankenstein
1. no, i cant open it with an xml editor, however it opens up perfectly well with plist editor...Marksman
2. the file is there 3.i dont know how to make sure there is no blank line 4.the program has permissionMarksman
A
1

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.

Abijah answered 5/11, 2011 at 15:45 Comment(0)
C
0

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.
Concernment answered 28/8, 2012 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.