Allow unordered xml nodes, Ignore *Specified fields, Convert byte[] to base64
This commit is contained in:
1 parent
93a415205e
commit
f563bae495
3 files changed
+177
-87
No files matched your search
@@ -55,6 +55,7 @@ namespace VocaluxeLib.Xml
|
|||||||
public bool IsList; //List with child elements (<List><El/><El/></List>)
|
public bool IsList; //List with child elements (<List><El/><El/></List>)
|
||||||
public bool IsEmbeddedList; //List w/o child elements(<List/><List/>)
|
public bool IsEmbeddedList; //List w/o child elements(<List/><List/>)
|
||||||
public bool IsDictionary; //List where the element names are the keys
|
public bool IsDictionary; //List where the element names are the keys
|
||||||
|
public bool IsByteArray; // Byte arrays w/o XmlArrayAttribute are serialized as Base64-Encoded strings
|
||||||
public bool IsNullable;
|
public bool IsNullable;
|
||||||
public bool IsNormalized;
|
public bool IsNormalized;
|
||||||
public XmlRangedAttribute Ranged;
|
public XmlRangedAttribute Ranged;
|
||||||
@@ -83,7 +84,7 @@ namespace VocaluxeLib.Xml
|
|||||||
|
|
||||||
public object GetValue(object o)
|
public object GetValue(object o)
|
||||||
{
|
{
|
||||||
return (_Field != null) ?_Field.GetValue(o) :_Property.GetValue(o, new object[] {});
|
return (_Field != null) ? _Field.GetValue(o) : _Property.GetValue(o, new object[] {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +133,10 @@ namespace VocaluxeLib.Xml
|
|||||||
if (!info.IsList)
|
if (!info.IsList)
|
||||||
{
|
{
|
||||||
Debug.Assert(!field.HasAttribute<XmlArrayAttribute>(), "A field cannot have an XmlElement- and XmlArray-Attribute");
|
Debug.Assert(!field.HasAttribute<XmlArrayAttribute>(), "A field cannot have an XmlElement- and XmlArray-Attribute");
|
||||||
info.IsEmbeddedList = true;
|
if (info.Type.IsArray && info.SubType == typeof(byte))
|
||||||
|
info.IsByteArray = true;
|
||||||
|
else
|
||||||
|
info.IsEmbeddedList = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -181,7 +185,7 @@ namespace VocaluxeLib.Xml
|
|||||||
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
|
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
|
||||||
foreach (FieldInfo field in fields)
|
foreach (FieldInfo field in fields)
|
||||||
{
|
{
|
||||||
if (field.HasAttribute<XmlIgnoreAttribute>())
|
if (field.HasAttribute<XmlIgnoreAttribute>() || field.Name.EndsWith("Specified"))
|
||||||
continue;
|
continue;
|
||||||
SFieldInfo info = new SFieldInfo(field);
|
SFieldInfo info = new SFieldInfo(field);
|
||||||
_FillInfo(ref info, field);
|
_FillInfo(ref info, field);
|
||||||
@@ -190,7 +194,7 @@ namespace VocaluxeLib.Xml
|
|||||||
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||||
foreach (PropertyInfo property in properties)
|
foreach (PropertyInfo property in properties)
|
||||||
{
|
{
|
||||||
if (property.HasAttribute<XmlIgnoreAttribute>())
|
if (property.HasAttribute<XmlIgnoreAttribute>() || property.Name.EndsWith("Specified") || !property.CanRead || !property.CanWrite)
|
||||||
continue;
|
continue;
|
||||||
SFieldInfo info = new SFieldInfo(property);
|
SFieldInfo info = new SFieldInfo(property);
|
||||||
_FillInfo(ref info, property);
|
_FillInfo(ref info, property);
|
||||||
|
|||||||
@@ -227,55 +227,47 @@ namespace VocaluxeLib.Xml
|
|||||||
nodes = parent.ChildNodes;
|
nodes = parent.ChildNodes;
|
||||||
|
|
||||||
List<SFieldInfo> fields = result.GetType().GetFields(attributes);
|
List<SFieldInfo> fields = result.GetType().GetFields(attributes);
|
||||||
int curField = 0;
|
|
||||||
|
|
||||||
if (nodes != null)
|
if (nodes != null)
|
||||||
{
|
{
|
||||||
List<object> subValues = new List<object>();
|
//Dictionary of all embedded lists to allow interleaved/mixed elements
|
||||||
SFieldInfo curListField = new SFieldInfo();
|
Dictionary<string, Tuple<SFieldInfo, List<object>>> embLists = new Dictionary<string, Tuple<SFieldInfo, List<object>>>();
|
||||||
string curListName = null;
|
|
||||||
foreach (XmlNode node in nodes)
|
foreach (XmlNode node in nodes)
|
||||||
{
|
{
|
||||||
if (node is XmlComment)
|
if (node is XmlComment)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (curListName != null)
|
|
||||||
{
|
|
||||||
if (node.Name != curListName && !node.Name.StartsWith(curListField.Name))
|
|
||||||
{
|
|
||||||
// End an embedded List
|
|
||||||
_AddList(result, curListField, subValues);
|
|
||||||
curField++;
|
|
||||||
curListName = null;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Continue an embedded List
|
|
||||||
object subValue = _GetValue(node, curListField.SubType);
|
|
||||||
subValues.Add(subValue);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SFieldInfo field = new SFieldInfo();
|
SFieldInfo field = new SFieldInfo();
|
||||||
for (; curField < fields.Count; curField++)
|
int curField;
|
||||||
|
for (curField = 0; curField < fields.Count; curField++)
|
||||||
{
|
{
|
||||||
field = fields[curField];
|
field = fields[curField];
|
||||||
if (field.Name == node.Name || field.AltName == node.Name || (field.IsEmbeddedList && node.Name.StartsWith(field.Name)))
|
if (field.Name == node.Name || field.AltName == node.Name || (field.IsEmbeddedList && node.Name.StartsWith(field.Name)))
|
||||||
break;
|
break;
|
||||||
if (!_CheckAndSetDefaultValue(result, field))
|
|
||||||
throw new XmlException("Unexpected element: " + _GetXPath(node) + "; Expected: " + field.Name);
|
|
||||||
}
|
}
|
||||||
if (curField >= fields.Count)
|
if (curField >= fields.Count)
|
||||||
throw new XmlException("Unexpected element: " + _GetXPath(node));
|
|
||||||
|
|
||||||
if (field.IsEmbeddedList)
|
|
||||||
{
|
{
|
||||||
// Start an embedded List
|
string msg = "Unexpected element: " + _GetXPath(node);
|
||||||
curListField = field;
|
if (fields.Count > 0)
|
||||||
curListName = field.Name;
|
msg += " Expected: " + string.Join(", ", fields.Select(f => f.Name));
|
||||||
subValues.Clear();
|
throw new XmlException(msg);
|
||||||
object subValue = _GetValue(node, curListField.SubType);
|
}
|
||||||
subValues.Add(subValue);
|
|
||||||
|
if (field.IsByteArray)
|
||||||
|
{
|
||||||
|
field.SetValue(result, Convert.FromBase64String(node.InnerText));
|
||||||
|
fields.RemoveAt(curField);
|
||||||
|
}
|
||||||
|
else if (field.IsEmbeddedList)
|
||||||
|
{
|
||||||
|
Tuple<SFieldInfo, List<object>> entry;
|
||||||
|
if (!embLists.TryGetValue(field.Name, out entry))
|
||||||
|
{
|
||||||
|
entry = new Tuple<SFieldInfo, List<object>>(field, new List<object>());
|
||||||
|
embLists.Add(field.Name, entry);
|
||||||
|
}
|
||||||
|
object subValue = _GetValue(node, field.SubType);
|
||||||
|
entry.Item2.Add(subValue);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -304,19 +296,18 @@ namespace VocaluxeLib.Xml
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
field.SetValue(result, value);
|
field.SetValue(result, value);
|
||||||
curField++;
|
fields.RemoveAt(curField);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (curListName != null)
|
//Add embedded lists
|
||||||
|
foreach (Tuple<SFieldInfo, List<object>> entry in embLists.Values)
|
||||||
{
|
{
|
||||||
// End an embedded List
|
_AddList(result, entry.Item1, entry.Item2);
|
||||||
_AddList(result, curListField, subValues);
|
fields.Remove(entry.Item1);
|
||||||
curField++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (; curField < fields.Count; curField++)
|
foreach (SFieldInfo field in fields)
|
||||||
{
|
{
|
||||||
SFieldInfo field = fields[curField];
|
|
||||||
if (!_CheckAndSetDefaultValue(result, field))
|
if (!_CheckAndSetDefaultValue(result, field))
|
||||||
throw new XmlException("element: " + field.Name + " is missing in " + _GetXPath(parent));
|
throw new XmlException("element: " + field.Name + " is missing in " + _GetXPath(parent));
|
||||||
}
|
}
|
||||||
@@ -395,6 +386,8 @@ namespace VocaluxeLib.Xml
|
|||||||
foreach (object subValue in values)
|
foreach (object subValue in values)
|
||||||
_WriteNode(writer, field.Name, field.SubType, subValue, field.IsAttribute);
|
_WriteNode(writer, field.Name, field.SubType, subValue, field.IsAttribute);
|
||||||
}
|
}
|
||||||
|
else if (field.IsByteArray)
|
||||||
|
writer.WriteElementString(field.Name, Convert.ToBase64String((byte[])value));
|
||||||
else
|
else
|
||||||
_WriteNode(writer, field.Name, field.Type, value, field.IsAttribute, field.ArrayItemName);
|
_WriteNode(writer, field.Name, field.Type, value, field.IsAttribute, field.ArrayItemName);
|
||||||
}
|
}
|
||||||
@@ -410,11 +403,23 @@ namespace VocaluxeLib.Xml
|
|||||||
xDoc.Load(reader);
|
xDoc.Load(reader);
|
||||||
if (xDoc.DocumentElement == null)
|
if (xDoc.DocumentElement == null)
|
||||||
throw new XmlException("No root element found!");
|
throw new XmlException("No root element found!");
|
||||||
_ReadChildNodes(xDoc.DocumentElement, ref result, false);
|
try
|
||||||
|
{
|
||||||
|
_ReadChildNodes(xDoc.DocumentElement, ref result, false);
|
||||||
|
}
|
||||||
|
catch (TargetInvocationException e)
|
||||||
|
{
|
||||||
|
throw e.InnerException ?? e;
|
||||||
|
}
|
||||||
return (T)result;
|
return (T)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T DeserializeString<T>(string xml) where T : new()
|
public T DeserializeString<T>(string xml) where T : new()
|
||||||
|
{
|
||||||
|
return DeserializeString(xml, new T());
|
||||||
|
}
|
||||||
|
|
||||||
|
public T DeserializeString<T>(string xml, T o) where T : new()
|
||||||
{
|
{
|
||||||
var reader = new XmlTextReader(new StringReader(xml))
|
var reader = new XmlTextReader(new StringReader(xml))
|
||||||
{
|
{
|
||||||
@@ -424,7 +429,7 @@ namespace VocaluxeLib.Xml
|
|||||||
};
|
};
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return _Deserialize<T>(reader, new T());
|
return _Deserialize<T>(reader, o);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -432,11 +437,15 @@ namespace VocaluxeLib.Xml
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Deserialize<T>(string filePath, object o = null) where T : new()
|
public T Deserialize<T>(string filePath) where T : new()
|
||||||
{
|
{
|
||||||
Debug.Assert(o == null || o.GetType() is T);
|
return Deserialize(filePath, new T());
|
||||||
if (o == null)
|
}
|
||||||
o = new T();
|
|
||||||
|
public T Deserialize<T>(string filePath, T o) where T : new()
|
||||||
|
{
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
throw new FileNotFoundException(filePath);
|
||||||
var reader = new XmlTextReader(filePath)
|
var reader = new XmlTextReader(filePath)
|
||||||
{
|
{
|
||||||
WhitespaceHandling = WhitespaceHandling.Significant,
|
WhitespaceHandling = WhitespaceHandling.Significant,
|
||||||
|
|||||||
@@ -71,26 +71,26 @@ namespace VocaluxeTests
|
|||||||
public int Auto { get; set; }
|
public int Auto { get; set; }
|
||||||
// ReSharper restore UnusedAutoPropertyAccessor.Local
|
// ReSharper restore UnusedAutoPropertyAccessor.Local
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct SBytes
|
||||||
|
{
|
||||||
|
public byte[] B;
|
||||||
|
}
|
||||||
|
|
||||||
|
private struct SIgnore
|
||||||
|
{
|
||||||
|
[XmlIgnore] public int I;
|
||||||
|
public int J;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class CIgnore
|
||||||
|
{
|
||||||
|
[XmlIgnore] public int I;
|
||||||
|
public int J;
|
||||||
|
}
|
||||||
#pragma warning restore 169
|
#pragma warning restore 169
|
||||||
#pragma warning restore 649
|
#pragma warning restore 649
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void TestBasic()
|
|
||||||
{
|
|
||||||
const string s = @"<root>
|
|
||||||
<I>1</I>
|
|
||||||
<S>2</S>
|
|
||||||
<F>3</F>
|
|
||||||
<D>4</D>
|
|
||||||
</root>";
|
|
||||||
var xml = new CXmlSerializer();
|
|
||||||
SBasic foo = xml.DeserializeString<SBasic>(s);
|
|
||||||
Assert.AreEqual(1, foo.I);
|
|
||||||
Assert.AreEqual("2", foo.S);
|
|
||||||
Assert.AreEqual(3, foo.F, 0.0001);
|
|
||||||
Assert.AreEqual(4, foo.D, 0.0001);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void _AssertFail<T>(Action test) where T : Exception
|
private static void _AssertFail<T>(Action test) where T : Exception
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -104,6 +104,49 @@ namespace VocaluxeTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void _AssertFail<T, T2>(String xmlString) where T2 : Exception where T : new()
|
||||||
|
{
|
||||||
|
var xml = new CXmlSerializer();
|
||||||
|
_AssertFail<T2>(() => xml.DeserializeString<T>(xmlString));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static T _AssertSerDeserMatch<T>(string xmlString) where T : new()
|
||||||
|
{
|
||||||
|
var xml = new CXmlSerializer();
|
||||||
|
T foo = xml.DeserializeString<T>(xmlString);
|
||||||
|
string xmlNew = xml.Serialize(foo);
|
||||||
|
Assert.AreEqual(xmlString, xmlNew);
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestBasic()
|
||||||
|
{
|
||||||
|
const string s = @"<root>
|
||||||
|
<I>1</I>
|
||||||
|
<S>2</S>
|
||||||
|
<F>3</F>
|
||||||
|
<D>4</D>
|
||||||
|
</root>";
|
||||||
|
const string sUnordered = @"<root>
|
||||||
|
<D>4</D>
|
||||||
|
<I>1</I>
|
||||||
|
<F>3</F>
|
||||||
|
<S>2</S>
|
||||||
|
</root>";
|
||||||
|
var xml = new CXmlSerializer();
|
||||||
|
SBasic foo = xml.DeserializeString<SBasic>(s);
|
||||||
|
Assert.AreEqual(1, foo.I);
|
||||||
|
Assert.AreEqual("2", foo.S);
|
||||||
|
Assert.AreEqual(3, foo.F, 0.0001);
|
||||||
|
Assert.AreEqual(4, foo.D, 0.0001);
|
||||||
|
foo = xml.DeserializeString<SBasic>(sUnordered);
|
||||||
|
Assert.AreEqual(1, foo.I);
|
||||||
|
Assert.AreEqual("2", foo.S);
|
||||||
|
Assert.AreEqual(3, foo.F, 0.0001);
|
||||||
|
Assert.AreEqual(4, foo.D, 0.0001);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestMissingXmlElement()
|
public void TestMissingXmlElement()
|
||||||
{
|
{
|
||||||
@@ -120,11 +163,10 @@ namespace VocaluxeTests
|
|||||||
<S>2</S>
|
<S>2</S>
|
||||||
<F>3</F>
|
<F>3</F>
|
||||||
</root>"};
|
</root>"};
|
||||||
var xml = new CXmlSerializer();
|
|
||||||
foreach (string s1 in s)
|
foreach (string s1 in s)
|
||||||
{
|
{
|
||||||
string sTmp = s1;
|
string sTmp = s1;
|
||||||
_AssertFail<XmlException>(() => xml.DeserializeString<SBasic>(sTmp));
|
_AssertFail<SBasic, XmlException>(sTmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,11 +192,10 @@ namespace VocaluxeTests
|
|||||||
<D>4</D>
|
<D>4</D>
|
||||||
<Foo>1</Foo>
|
<Foo>1</Foo>
|
||||||
</root>"};
|
</root>"};
|
||||||
var xml = new CXmlSerializer();
|
|
||||||
foreach (string s1 in s)
|
foreach (string s1 in s)
|
||||||
{
|
{
|
||||||
string sTmp = s1;
|
string sTmp = s1;
|
||||||
_AssertFail<XmlException>(() => xml.DeserializeString<SBasic>(sTmp));
|
_AssertFail<SBasic, XmlException>(sTmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,10 +208,7 @@ namespace VocaluxeTests
|
|||||||
<F>3</F>
|
<F>3</F>
|
||||||
<D>4</D>
|
<D>4</D>
|
||||||
</root>";
|
</root>";
|
||||||
var xml = new CXmlSerializer();
|
_AssertSerDeserMatch<SBasic>(s);
|
||||||
SBasic foo = xml.DeserializeString<SBasic>(s);
|
|
||||||
string s2 = xml.Serialize(foo);
|
|
||||||
Assert.AreEqual(s, s2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly string[] _XMLList = new string[]
|
private readonly string[] _XMLList = new string[]
|
||||||
@@ -212,7 +250,7 @@ namespace VocaluxeTests
|
|||||||
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed");
|
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed");
|
||||||
res = xml.Serialize(foo);
|
res = xml.Serialize(foo);
|
||||||
Assert.AreEqual(_XMLList[2], res, "Serialization2 failed");
|
Assert.AreEqual(_XMLList[2], res, "Serialization2 failed");
|
||||||
_AssertFail<XmlException>(() => xml.DeserializeString<SList>(_Empty));
|
_AssertFail<SList, XmlException>(_Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -233,7 +271,7 @@ namespace VocaluxeTests
|
|||||||
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed");
|
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed");
|
||||||
res = xml.Serialize(foo);
|
res = xml.Serialize(foo);
|
||||||
Assert.AreEqual(_XMLList[2], res, "Serialization2 failed");
|
Assert.AreEqual(_XMLList[2], res, "Serialization2 failed");
|
||||||
_AssertFail<XmlException>(() => xml.DeserializeString<SArray>(_Empty));
|
_AssertFail<SArray, XmlException>(_Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private const string _XMLListEmb = _Head + @"<root>
|
private const string _XMLListEmb = _Head + @"<root>
|
||||||
@@ -276,7 +314,7 @@ namespace VocaluxeTests
|
|||||||
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization failed");
|
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization failed");
|
||||||
res = xml.Serialize(foo);
|
res = xml.Serialize(foo);
|
||||||
Assert.AreEqual(_XMLListEmb3, res, "Serialization failed");
|
Assert.AreEqual(_XMLListEmb3, res, "Serialization failed");
|
||||||
_AssertFail<XmlException>(() => xml.DeserializeString<SListEmb>(_Empty));
|
_AssertFail<SListEmb, XmlException>(_Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -297,23 +335,62 @@ namespace VocaluxeTests
|
|||||||
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization failed");
|
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization failed");
|
||||||
res = xml.Serialize(foo);
|
res = xml.Serialize(foo);
|
||||||
Assert.AreEqual(_XMLListEmb3, res, "Serialization failed");
|
Assert.AreEqual(_XMLListEmb3, res, "Serialization failed");
|
||||||
_AssertFail<XmlException>(() => xml.DeserializeString<SArrayEmb>(_Empty));
|
_AssertFail<SArrayEmb, XmlException>(_Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private const string _XmlProperty = _Head + @"<root>
|
|
||||||
<Public>2</Public>
|
|
||||||
<Auto>3</Auto>
|
|
||||||
</root>";
|
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestProperty()
|
public void TestProperty()
|
||||||
{
|
{
|
||||||
|
const string xmlString = _Head + @"<root>
|
||||||
|
<Public>2</Public>
|
||||||
|
<Auto>3</Auto>
|
||||||
|
</root>";
|
||||||
var xml = new CXmlSerializer();
|
var xml = new CXmlSerializer();
|
||||||
SProperty foo = xml.DeserializeString<SProperty>(_XmlProperty);
|
SProperty foo = xml.DeserializeString<SProperty>(xmlString);
|
||||||
Assert.AreEqual(3, foo.Private);
|
Assert.AreEqual(3, foo.Private);
|
||||||
Assert.AreEqual(3, foo.Auto);
|
Assert.AreEqual(3, foo.Auto);
|
||||||
string newXml = xml.Serialize(foo);
|
string newXml = xml.Serialize(foo);
|
||||||
Assert.AreEqual(_XmlProperty, newXml);
|
Assert.AreEqual(xmlString, newXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestByteArray()
|
||||||
|
{
|
||||||
|
const string xmlString = _Head + @"<root>
|
||||||
|
<B>MTMzNw==</B>
|
||||||
|
</root>";
|
||||||
|
_AssertSerDeserMatch<SBytes>(xmlString);
|
||||||
|
}
|
||||||
|
|
||||||
|
private const string _XmlIgnore = _Head + @"<root>
|
||||||
|
<J>2</J>
|
||||||
|
</root>";
|
||||||
|
private const string _XmlIgnore2 = _Head + @"<root>
|
||||||
|
<I>2</I>
|
||||||
|
<J>2</J>
|
||||||
|
</root>";
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestIgnore()
|
||||||
|
{
|
||||||
|
_AssertSerDeserMatch<SIgnore>(_XmlIgnore);
|
||||||
|
_AssertFail<SIgnore, XmlException>(_XmlIgnore2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestExisting()
|
||||||
|
{
|
||||||
|
var foo = new SIgnore {I = 1};
|
||||||
|
var xml = new CXmlSerializer();
|
||||||
|
SIgnore bar = xml.DeserializeString(_XmlIgnore, foo);
|
||||||
|
Assert.AreEqual(1, bar.I);
|
||||||
|
Assert.AreEqual(_XmlIgnore, xml.Serialize(bar));
|
||||||
|
|
||||||
|
var foo2 = new CIgnore {I = 1};
|
||||||
|
CIgnore bar2 = xml.DeserializeString(_XmlIgnore, foo2);
|
||||||
|
Assert.AreEqual(1, bar2.I);
|
||||||
|
Assert.AreEqual(foo2.J, bar2.J, "Original classes should be modified by the deserialization");
|
||||||
|
Assert.AreEqual(_XmlIgnore, xml.Serialize(bar2));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -325,7 +402,7 @@ namespace VocaluxeTests
|
|||||||
{
|
{
|
||||||
string xmlPath = Path.Combine(filePath, type.Name + ".xml");
|
string xmlPath = Path.Combine(filePath, type.Name + ".xml");
|
||||||
var xml = new CXmlSerializer(type == typeof(CConfig.SConfig));
|
var xml = new CXmlSerializer(type == typeof(CConfig.SConfig));
|
||||||
object foo = xml.GetType().GetMethod("Deserialize").MakeGenericMethod(new Type[] {type}).Invoke(xml, new object[] {xmlPath});
|
object foo = xml.Deserialize(xmlPath, Activator.CreateInstance(type));
|
||||||
Assert.IsInstanceOfType(foo, type);
|
Assert.IsInstanceOfType(foo, type);
|
||||||
string newXml = xml.Serialize(foo);
|
string newXml = xml.Serialize(foo);
|
||||||
string oldXml = File.ReadAllText(xmlPath);
|
string oldXml = File.ReadAllText(xmlPath);
|
||||||
|
|||||||
Reference in new issue
Block a user