I have a class, which object I serialize to an XML string. no prob.
Deserializing also works but it sets a field that is the XML "true" to false (probably because it can not convert to boolean true.
So I decorated that property with
public class X
{
// ...
private bool _status = false;
[XmlText]
public bool Status {
get {return _status;}
set {_status=value;}
}
// ...
}
However then I get "xmlserializer - there was an error reflecting type X" ...
So what is the workaround other than replace all my checks against a string Status instead?
ref: - XmlSerializer - There was an error reflecting type - Deserialize boolean element with string attribute
update on request: The serialize/deserialize class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// for serializer:
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace StackOverflow
{
static class Serializing
{
public static T XmlDeserializeFromString<T>(this string objectData)
{
return (T)XmlDeserializeFromString(objectData, typeof(T));
}
public static object XmlDeserializeFromString(this string objectData, Type type)
{
var serializer = new XmlSerializer(type);
object result;
using (TextReader reader = new StringReader(objectData))
{
result = serializer.Deserialize(reader);
}
return result;
}
public static string Serialize<T>(this T value)
{
if (value == null) { return string.Empty; }
try
{
var xmlserializer = new XmlSerializer(typeof(User));
var stringWriter = new StringWriter();
using (var writer = XmlWriter.Create(stringWriter))
{
xmlserializer.Serialize(writer, value);
return stringWriter.ToString();
}
}
catch (Exception e)
{
throw new Exception("A Serialization Error Occurred", e);
}
}
}
}