Skip to content

Instantly share code, notes, and snippets.

@kosmakoff
Created May 11, 2016 08:42
Show Gist options
  • Save kosmakoff/1da2424447566c8c64de9d8fa1c6d32a to your computer and use it in GitHub Desktop.
Save kosmakoff/1da2424447566c8c64de9d8fa1c6d32a to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
namespace PlistDemo
{
class Program
{
static void Main(string[] args)
{
const string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
<plist version=""1.0"">
<dict>
<key>Last Backup Date</key>
<date>2016-04-06T10:16:17Z</date>
</dict>
</plist>
";
IntPtr dictPtr;
Plist.plist_from_xml(xml, (uint) xml.Length, out dictPtr);
var backupDatePtr = Plist.plist_dict_get_item(dictPtr, "Last Backup Date");
int sec, usec;
Plist.plist_get_date_val(backupDatePtr, out sec, out usec);
var date = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(sec);
Console.WriteLine($"Parsing XML:\n{xml}\n\n=======================");
Console.WriteLine($"{date} ({date.Kind}) from secs: {sec}");
}
}
internal static class Plist
{
private const string DllName = @"C:\Programs\imobiledevice\libplist.dll";
[DllImport(DllName, EntryPoint = "plist_from_xml", CallingConvention = CallingConvention.Cdecl)]
public static extern void plist_from_xml([MarshalAs(UnmanagedType.LPStr)]string xml, uint length, out IntPtr nodePtr);
[DllImport(DllName, EntryPoint = "plist_dict_get_item", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr plist_dict_get_item(IntPtr node, [In, MarshalAs(UnmanagedType.LPStr)] string key);
[DllImport(DllName, EntryPoint = "plist_get_date_val", CallingConvention = CallingConvention.Cdecl)]
public static extern void plist_get_date_val(IntPtr node, out int sec, out int usec);
}
}
@kosmakoff
Copy link
Author

The output of the program is:

Parsing XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Last Backup Date</key>
    <date>2016-04-06T10:16:17Z</date>
</dict>
</plist>


=======================
06.04.2016 11:16:17 (Utc) from secs: 481634177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment