I have to XML (de)serialize the following class:

this gives the following output:
<ArrayOfPropertyFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PropertyFilter>
<AndOr>And</AndOr>
<LeftBracket>None</LeftBracket>
<Property>17</Property>
<Operator>Equal</Operator>
<Value xsi:type="xsd:string">lll</Value>
<RightBracket>None</RightBracket>
</PropertyFilter>
</ArrayOfPropertyFilter>
and, after deserialization it gives

How can I “tell” to Serializer to keep the Value “as is”, without any XML node….(in the concrete case the Value should be “lll” and not XMLNode containing Text “lll”) ?
EDIT
Bellow is a full working sample in C#. The output is
Value is = ‘System.Xml.XmlNode[]’
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PropertyFilter filter = new PropertyFilter();
filter.AndOr = "Jora";
filter.Value = "haha";
filter.Property = 15;
var xml = filter.SerializeToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
PropertyFilter cloneFilter = xmlDoc.Deserialize<PropertyFilter>();
Console.WriteLine("Value is = '{0}'", cloneFilter.Value);
}
}
public class PropertyFilter
{
public string AndOr { get; set; }
public string LeftBracket { get; set; }
public int Property { get; set; }
public string Operator { get; set; }
public object Value { get; set; }
public string RightBracket { get; set; }
}
public static class Utils
{
public static string SerializeToString(this object instance)
{
if (instance == null)
throw new ArgumentNullException("instance");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlSerializer serializer = new XmlSerializer(
instance.GetType(), null, new Type[0], null, null);
serializer.Serialize(sw, instance);
return sb.ToString();
}
public static T Deserialize<T>(this XmlDocument xmlDoc)
{
XmlNodeReader reader = new XmlNodeReader(xmlDoc.DocumentElement);
XmlSerializer serializer = new XmlSerializer(typeof(T));
object obj = serializer.Deserialize(reader);
T myT = (T)obj;
return myT;
}
}
}
EDIT 2
To stress the Anton answer, the second example (updated with the Groo’s remarks):
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PropertyFilter filter = new PropertyFilter();
filter.AndOr = "Jora";
var obj = new Hehe();
obj.Behehe = 4526;
filter.Value = obj;
filter.Property = 15;
var xml = filter.SerializeToString();
PropertyFilter cloneFilter = xml.Deserialize<PropertyFilter>();
Console.WriteLine("Value is = '{0}'", cloneFilter.Value);
}
}
public class Hehe
{
public int Behehe { get; set; }
public override string ToString()
{
return string.Format("behehe is '{0}'", Behehe);
}
}
public class PropertyFilter
{
public string AndOr { get; set; }
public string LeftBracket { get; set; }
public int Property { get; set; }
public string Operator { get; set; }
//[XmlElement(typeof(Hehe))]
public object Value { get; set; }
public string RightBracket { get; set; }
}
public static class Utils
{
public static string SerializeToString(this object instance)
{
if (instance == null)
throw new ArgumentNullException("instance");
StringBuilder sb = new StringBuilder();
XmlSerializer serializer = new XmlSerializer(instance.GetType(), null, new Type[0], null, null);
using (StringWriter sw = new StringWriter(sb))
{
serializer.Serialize(sw, instance);
}
return sb.ToString();
}
public static T Deserialize<T>(this string xmlString)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(xmlString))
{
return (T)serializer.Deserialize(sr);
}
}
}
}
Gree’s solution will work as long as
Valuetakes values of primitive, XSD-defined types likestringandint, or user-defined types mentioned somewhere inT‘s definition (Titself, the types of its properties etc.) As soon as you need to deserialize a value of a type different from these, you must declare all possible types ofValuewithXmlElementAttribute, e.g.