Serialize to/from string

This commit is contained in:
Flamefire committed 2014-10-27 17:51:06 +01:00
1 parent acaf4eadb2
commit e982d7ee6f
2 files changed
+46 -11

No files matched your search

+2 -1
View File
@@ -196,7 +196,8 @@ namespace Vocaluxe.Base
Path.Combine(CSettings.ProgramFolder, CSettings.FolderNameSongs) Path.Combine(CSettings.ProgramFolder, CSettings.FolderNameSongs)
#if INSTALLER #if INSTALLER
,Path.Combine(CSettings.DataFolder, CSettings.FolderNameSongs); ,
Path.Combine(CSettings.DataFolder, CSettings.FolderNameSongs)
#endif #endif
}; };
/// <summary> /// <summary>
+44 -10
View File
@@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
@@ -102,6 +103,8 @@ namespace VocaluxeLib.Xml
return _GetPrimitiveValue(node, type); return _GetPrimitiveValue(node, type);
if (type.IsNullable()) if (type.IsNullable())
{ {
if (!node.HasChildNodes)
return null;
Type subType = type.GetGenericArguments()[0]; Type subType = type.GetGenericArguments()[0];
return _GetValue(node, subType); return _GetValue(node, subType);
} }
@@ -372,7 +375,8 @@ namespace VocaluxeLib.Xml
{ {
Debug.Assert(!isAttribute, "Complex types cannot be attributes"); Debug.Assert(!isAttribute, "Complex types cannot be attributes");
writer.WriteStartElement(name); writer.WriteStartElement(name);
_WriteChildNodes(writer, value); if (value != null)
_WriteChildNodes(writer, value);
writer.WriteEndElement(); writer.WriteEndElement();
} }
} }
@@ -411,6 +415,24 @@ namespace VocaluxeLib.Xml
return (T)result; return (T)result;
} }
public T DeserializeString<T>(string xml) where T : new()
{
var reader = new XmlTextReader(new StringReader(xml))
{
WhitespaceHandling = WhitespaceHandling.Significant,
Normalization = true,
XmlResolver = null
};
try
{
return _Deserialize<T>(reader);
}
finally
{
reader.Close();
}
}
public T Deserialize<T>(string filePath) where T : new() public T Deserialize<T>(string filePath) where T : new()
{ {
var reader = new XmlTextReader(filePath) var reader = new XmlTextReader(filePath)
@@ -429,7 +451,7 @@ namespace VocaluxeLib.Xml
} }
} }
public void Serialize(string filePath, object o) private void _Serialize(object o, XmlWriter writer)
{ {
XmlRootAttribute root = o.GetType().GetAttribute<XmlRootAttribute>(); XmlRootAttribute root = o.GetType().GetAttribute<XmlRootAttribute>();
string name; string name;
@@ -443,15 +465,27 @@ namespace VocaluxeLib.Xml
else else
name = "root"; name = "root";
} }
writer.WriteStartDocument();
writer.WriteStartElement(name);
_WriteChildNodes(writer, o);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
public void Serialize(string filePath, object o)
{
using (XmlWriter writer = XmlWriter.Create(filePath, _XMLSettings)) using (XmlWriter writer = XmlWriter.Create(filePath, _XMLSettings))
{ _Serialize(o, writer);
writer.WriteStartDocument(); }
writer.WriteStartElement(name);
_WriteChildNodes(writer, o); public string Serialize(object o)
writer.WriteEndElement(); {
writer.WriteEndDocument(); MemoryStream result = new MemoryStream();
writer.Flush(); using (XmlWriter writer = XmlWriter.Create(result, _XMLSettings))
} _Serialize(o, writer);
result.Position = 0;
return new StreamReader(result).ReadToEnd();
} }
} }
} }