2

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?

moonlander
  • 143
  • 2
  • 8
  • what do you need from the TextRange object? – Headhunter Xamd Nov 11 '16 at 12:28
  • I removed a tag from your question's title. See [here](http://stackoverflow.com/help/tagging) why. – dymanoid Nov 11 '16 at 12:29
  • @HeadhunterXamd the text from a RichTextBox with the formatting. – moonlander Nov 11 '16 at 12:30
  • @moonlander you mean the full textbox or the selected text? If you need the selected text its the Text property of the TextRange instance. That is just a string so you can easily construct something for it. https://msdn.microsoft.com/en-us/library/system.windows.documents.textrange(v=vs.110).aspx#Anchor_3 – Headhunter Xamd Nov 11 '16 at 12:32
  • @HeadhunterXamd i need the whole text that's inside the RichTextBox, including it's formatting (color, size etc...). – moonlander Nov 11 '16 at 12:39
  • then you can ignore the TextRange is it not? if so, just add the `[NonSerialized]` attribute to the TextRange field. – Headhunter Xamd Nov 11 '16 at 12:42
  • but i do want that TextRange to be serialized. it contains all the text that's in the RichTextBox, including the formatting of the text. The issue is that i can't figure out how to serialize both of the TextRange and the Text properties of my class into the same xml block. – moonlander Nov 11 '16 at 12:45
  • i meant both the TextRange and the SomeProp string property. – moonlander Nov 11 '16 at 12:56

1 Answers1

0

Well, for anyone interested. The problem was that i wanted to have the whole MyClass object serialized to one xml string. But the TextRange requires special serializing method like i wrote in the main post. So the workaround was:

  • Create a public helper class inside MyClass, say MyClassSerializationHelper.

  • Add two string properties to it, same as MyClass properties but as strings. In my case SomeProp and MyTextRange. Full properties (getters and setters).

To Serialize:

  • Create a MyClassSerializationHelper instance.

  • SomeProp gets the same value from MyClass SomeProp property, because it's a string.

  • MyTextRange gets the serialized string of the MyClass MyTextRange property using the special serialization method.

  • Serialize the whole MyClassSerializationHelper instance, you got an xml string.

To Deserialize:

  • Deserialize the xml string and get a MyClassSerializationHelper instace.

  • Deserialize the MyTextRange property (it's an xml string) to get a TextRange instance.

  • Rebuild a MyClass instance using the properties you deserialized.

Hope that's clear.

moonlander
  • 143
  • 2
  • 8