-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyBag.cs
More file actions
133 lines (119 loc) · 4.37 KB
/
PropertyBag.cs
File metadata and controls
133 lines (119 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Newtonsoft.Json;
namespace EZms.Core
{
/// <inheritdoc />
/// <summary>
/// Creates a serializable string/object dictionary that is XML serializable
/// Encodes keys as element names and values as simple values with a type
/// attribute that contains an XML type name. Complex names encode the type
/// name with type='___namespace.classname' format followed by a standard xml
/// serialized format. The latter serialization can be slow so it's not recommended
/// to pass complex types if performance is critical.
/// </summary>
[XmlRoot("properties")]
public class PropertyBag : PropertyBag<object>
{
/// <summary>
/// Creates an instance of a propertybag from an Xml string
/// </summary>
/// <param name="xml">Serialize</param>
/// <returns></returns>
public new static PropertyBag CreateFromXml(string xml)
{
var bag = new PropertyBag();
bag.FromXml(xml);
return bag;
}
}
/// <summary>
/// Creates a serializable string for generic types that is XML serializable.
/// Encodes keys as element names and values as simple values with a type
/// attribute that contains an XML type name. Complex names encode the type
/// name with type='___namespace.classname' format followed by a standard xml
/// serialized format. The latter serialization can be slow so it's not recommended
/// to pass complex types if performance is critical.
/// </summary>
/// <typeparam name="TValue">Must be a reference type. For value types use type object</typeparam>
[XmlRoot("properties")]
public class PropertyBag<TValue> : Dictionary<string, TValue>, IXmlSerializable
{
/// <summary>
/// Not implemented - this means no schema information is passed
/// so this won't work with ASMX/WCF services.
/// </summary>
/// <returns></returns>
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
/// <inheritdoc />
/// <summary>
/// Serializes the dictionary to XML. Keys are
/// serialized to element names and values as
/// element values. An xml type attribute is embedded
/// for each serialized element - a .NET type
/// element is embedded for each complex type and
/// prefixed with three underscores.
/// </summary>
/// <param name="writer"></param>
public void WriteXml(System.Xml.XmlWriter writer)
{
throw new NotImplementedException();
}
/// <inheritdoc />
/// <summary>
/// Reads the custom serialized format
/// </summary>
/// <param name="reader"></param>
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
/// <summary>
/// Serializes this dictionary to an XML string
/// </summary>
/// <returns>XML String or Null if it fails</returns>
public string ToXml()
{
return JsonConvert.SerializeObject(this);
}
/// <summary>
/// Deserializes from an XML string
/// </summary>
/// <param name="xml"></param>
/// <returns>true or false</returns>
public bool FromXml(string xml)
{
Clear();
// if xml string is empty we return an empty dictionary
if (string.IsNullOrEmpty(xml))
return true;
if (JsonConvert.DeserializeObject(xml,
GetType()) is PropertyBag<TValue> result)
{
foreach (var item in result)
{
Add(item.Key, item.Value);
}
}
else
// null is a failure
return false;
return true;
}
/// <summary>
/// Creates an instance of a propertybag from an Xml string
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static PropertyBag<TValue> CreateFromXml(string xml)
{
var bag = new PropertyBag<TValue>();
bag.FromXml(xml);
return bag;
}
}
}