I have an C# class that I would like to serialize using XMLSerializer. But I would like to have it serialized to a XMLElement or XMLDocument. Is this possible or do I have t
I had this problem too, and Matt Davis provided a great solution. Just posting some code snippets, since there are a few more details.
public static XmlElement SerializeToXmlElement(object o)
{
XmlDocument doc = new XmlDocument();
using(XmlWriter writer = doc.CreateNavigator().AppendChild())
{
new XmlSerializer(o.GetType()).Serialize(writer, o);
}
return doc.DocumentElement;
}
public static T DeserializeFromXmlElement(XmlElement element)
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(new XmlNodeReader(element));
}