views:

706

answers:

1

Hi

I want to save content of a RichTextBox to varbinary (= byte array) in XamlPackage format. I need technicial advise on how to it.

I actually need to know how to convert between FlowDocument to byte array.

Is it even recommended to store it as varbinary, or this is a bad idea?


Update

Code snippet:

///Load
byte[] document = GetDocumentFromDataBase();
RickTextBox tb = new RickTextBox();
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)
tr.Load(--------------------------) //Load from the byte array.


///Save
int maxAllowed = 1024;
byte[] document;
RichTextBox tb = new RichTextBox();
//User entered text and designs in the rich text
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)   
tr.Save(--------------------------) //Save to byte array
if (document.Length > maxAllowed) 
{
    MessageBox.Show((document.Length - maxAllowed) + " Exceeding limit.");
    return;
}
SaveToDataBase();
TextRange
A: 

I can't find my full example right now, but you can use XamlReader and XamlWriter to get the document into and out of a string. From there, you can use UnicodeEncoding, AsciiEncoding or whatever encoder you want to get it into and out of bytes.

My shorter example for setting the document from a string... docReader is my flow document reader

        private void SetDetails(string detailsString)
    {
        if (docReader == null)
            return;
        if (String.IsNullOrEmpty(detailsString))
        {
            this.docReader.Document = null;
            return;
        }
        using (
        StringReader stringReader = new StringReader(detailsString))
        {
            using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader))
            {
                this.docReader.Document = XamlReader.Load(reader) as FlowDocument;
            }
        }
    }
Jim Leonardo
Is it even recommended to store it as varbinary, or this is a bad idea?
Shimmy
I don't think it's a terribly bad idea, but I would definitely keep in mind that these can get big, so unless you're using varbinary(max) on a DB that supports that, you should consider a blob data type instead.
Jim Leonardo
I am not talking about the storage, I am asking about the performance, i.e. the unpacking of the byte array. about blobs: 1) i will not use large docs, 2) In this project I use 2005.
Shimmy
Also note, I added a code snippet to my question, please take a look at it.
Shimmy
Sorry for the late reply... I don't think you'll notice any real performance difference either way. I haven't done tons with this, but my experience in the RichTextBox in WPF so far is that the UI operations will probably be the largest chunk of the time and the gains you get by changing underlying types will probably be a small percentage of the time.I think what you have will probably work, but I haven't done it directly myself as doing the XamlReader.Load was more in line with what I was doing at the time. It looks basically ok though.
Jim Leonardo