I'm trying to serialize a class that has a property of type TextRange
in it.
Example:
public class MyClass
{
private string someProp;
public string SomeProp
{
get { return someProp; }
set { someProp = value; }
}
private TextRange myTextRange;
public TextRange MyTextRange
{
get { return myTextRange; }
set { myTextRange = value; }
}
}
The thing is, that the TextRange
type can't be serialized regularly while serializing the whole class, it has a special method of its own for serializing itself, i'm doing it like this:
using (MemoryStream ms = new MemoryStream())
{
myTextRange.Save(ms, DataFormats.Xaml, true);
string xaml = Encoding.ASCII.GetString(ms.ToArray());
}
The problem is that I want the class to be serialized into one string (xml string) with the TextRange
property and the other property together. I don't mind using another serialization method (not xml) but I don't see how it solves the problem.
Regularly I serialize the whole class at once, but the TextRange
class is not marked as serializable (no binary serilaization) and doesn't have an empty constructor (no xml serilization).
That's how I do it regularly:
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
StringWriter stringWriter = new StringWriter();
using (XmlWriter writer = XmlWriter.Create(stringWriter))
{
serializer.Serialize(writer, this);
string xml = stringWriter.ToString();
return xml;
}
How can I do this?