|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.IO; |
4 |
| -using System.Runtime.Serialization.Formatters.Binary; |
5 |
| -using System.Xml.XPath; |
6 |
| - |
7 |
| -namespace N2.Persistence.Serialization |
8 |
| -{ |
9 |
| - public abstract class XmlReader |
10 |
| - { |
11 |
| - public static Dictionary<string, string> GetAttributes(XPathNavigator navigator) |
12 |
| - { |
13 |
| - if (!navigator.MoveToFirstAttribute()) |
14 |
| - throw new DeserializationException("Node has no attributes: " + navigator.Name); |
15 |
| - Dictionary<string, string> attributes = new Dictionary<string, string>(); |
16 |
| - do |
17 |
| - { |
18 |
| - attributes.Add(navigator.Name, navigator.Value); |
19 |
| - } while (navigator.MoveToNextAttribute()); |
20 |
| - navigator.MoveToParent(); |
21 |
| - return attributes; |
22 |
| - } |
23 |
| - |
24 |
| - public static object Parse(string value, Type type) |
25 |
| - { |
26 |
| - if (type == typeof(object)) |
27 |
| - { |
28 |
| - byte[] buffer = Convert.FromBase64String(value); |
29 |
| - BinaryFormatter formatter = new BinaryFormatter(); |
30 |
| - return formatter.Deserialize(new MemoryStream(buffer)); |
31 |
| - } |
32 |
| - else if (type == typeof(DateTime)) |
33 |
| - { |
34 |
| - return ToNullableDateTime(value); |
35 |
| - } |
36 |
| - else |
37 |
| - return Utility.Convert(value, type); |
38 |
| - } |
39 |
| - |
40 |
| - public static IEnumerable<XPathNavigator> EnumerateChildren(XPathNavigator navigator) |
41 |
| - { |
42 |
| - if (navigator.MoveToFirstChild()) |
43 |
| - { |
44 |
| - do |
45 |
| - { |
46 |
| - yield return navigator; |
47 |
| - } while (navigator.MoveToNext()); |
48 |
| - |
49 |
| - navigator.MoveToParent(); |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - public static DateTime? ToNullableDateTime(string value) |
54 |
| - { |
55 |
| - DateTime _result; |
56 |
| - |
57 |
| - return DateTime.TryParse( |
58 |
| - value, |
59 |
| - System.Globalization.CultureInfo.InvariantCulture, |
60 |
| - System.Globalization.DateTimeStyles.None, |
61 |
| - out _result) |
62 |
| - ? _result.ToLocalTime() |
63 |
| - : default(DateTime?); |
64 |
| - } |
65 |
| - |
66 |
| - protected static void SetLinkedItem(string value, ReadingJournal journal, Action<ContentItem> setter, string versionKey = null) |
67 |
| - { |
68 |
| - int referencedItemID = int.Parse(value); |
69 |
| - |
70 |
| - if (referencedItemID != 0) |
71 |
| - { |
72 |
| - ContentItem referencedItem = journal.Find(referencedItemID); |
73 |
| - if (referencedItem != null) |
74 |
| - { |
75 |
| - setter(referencedItem); |
76 |
| - } |
77 |
| - else |
78 |
| - { |
79 |
| - journal.Register(referencedItemID, setter); |
80 |
| - } |
81 |
| - } |
82 |
| - else if (!string.IsNullOrEmpty(versionKey)) |
83 |
| - { |
84 |
| - |
85 |
| - ContentItem referencedItem = journal.Find(versionKey); |
86 |
| - if (referencedItem != null) |
87 |
| - { |
88 |
| - setter(referencedItem); |
89 |
| - } |
90 |
| - else |
91 |
| - { |
92 |
| - journal.Register(versionKey, setter); |
93 |
| - } |
94 |
| - } |
95 |
| - } |
96 |
| - } |
97 |
| -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Runtime.Serialization.Formatters.Binary; |
| 5 | +using System.Web; |
| 6 | +using System.Xml.XPath; |
| 7 | + |
| 8 | +namespace N2.Persistence.Serialization |
| 9 | +{ |
| 10 | + public abstract class XmlReader |
| 11 | + { |
| 12 | + public static Dictionary<string, string> GetAttributes(XPathNavigator navigator) |
| 13 | + { |
| 14 | + if (!navigator.MoveToFirstAttribute()) |
| 15 | + throw new DeserializationException("Node has no attributes: " + navigator.Name); |
| 16 | + Dictionary<string, string> attributes = new Dictionary<string, string>(); |
| 17 | + do |
| 18 | + { |
| 19 | + attributes.Add(navigator.Name, navigator.Value); |
| 20 | + } while (navigator.MoveToNextAttribute()); |
| 21 | + navigator.MoveToParent(); |
| 22 | + return attributes; |
| 23 | + } |
| 24 | + |
| 25 | + public static object Parse(string value, Type type) |
| 26 | + { |
| 27 | + if (type == typeof(object)) |
| 28 | + { |
| 29 | + byte[] buffer = Convert.FromBase64String(value); |
| 30 | + BinaryFormatter formatter = new BinaryFormatter(); |
| 31 | + return formatter.Deserialize(new MemoryStream(buffer)); |
| 32 | + } |
| 33 | + else if (type == typeof(DateTime)) |
| 34 | + { |
| 35 | + return ToNullableDateTime(value); |
| 36 | + } |
| 37 | + else if (type.IsEnum) { |
| 38 | + value = HttpUtility.HtmlDecode(value); |
| 39 | + return Utility.Convert(value, type); |
| 40 | + } |
| 41 | + else |
| 42 | + return Utility.Convert(value, type); |
| 43 | + } |
| 44 | + |
| 45 | + public static IEnumerable<XPathNavigator> EnumerateChildren(XPathNavigator navigator) |
| 46 | + { |
| 47 | + if (navigator.MoveToFirstChild()) |
| 48 | + { |
| 49 | + do |
| 50 | + { |
| 51 | + yield return navigator; |
| 52 | + } while (navigator.MoveToNext()); |
| 53 | + |
| 54 | + navigator.MoveToParent(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static DateTime? ToNullableDateTime(string value) |
| 59 | + { |
| 60 | + DateTime _result; |
| 61 | + |
| 62 | + return DateTime.TryParse( |
| 63 | + value, |
| 64 | + System.Globalization.CultureInfo.InvariantCulture, |
| 65 | + System.Globalization.DateTimeStyles.None, |
| 66 | + out _result) |
| 67 | + ? _result.ToLocalTime() |
| 68 | + : default(DateTime?); |
| 69 | + } |
| 70 | + |
| 71 | + protected static void SetLinkedItem(string value, ReadingJournal journal, Action<ContentItem> setter, string versionKey = null) |
| 72 | + { |
| 73 | + int referencedItemID = int.Parse(value); |
| 74 | + |
| 75 | + if (referencedItemID != 0) |
| 76 | + { |
| 77 | + ContentItem referencedItem = journal.Find(referencedItemID); |
| 78 | + if (referencedItem != null) |
| 79 | + { |
| 80 | + setter(referencedItem); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + journal.Register(referencedItemID, setter); |
| 85 | + } |
| 86 | + } |
| 87 | + else if (!string.IsNullOrEmpty(versionKey)) |
| 88 | + { |
| 89 | + |
| 90 | + ContentItem referencedItem = journal.Find(versionKey); |
| 91 | + if (referencedItem != null) |
| 92 | + { |
| 93 | + setter(referencedItem); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + journal.Register(versionKey, setter); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments