我试图节省一些C#源代码到数据库中。基本上我有一个RichTextBox用户可键入他们的代码并保存到数据库中。

当我复制和Visual Studio环境中贴,我想保留格式化等等。所以我选择到FlowDocuments XAML中保存到数据库中,并设置此回RichTextBox.Document。

我的下面2个函数序列化并deserialise的RTB的内容。

     private string GetXaml(FlowDocument document)
    {
        if (document == null) return String.Empty;
        else{
            StringBuilder sb = new StringBuilder();
            XmlWriter xw = XmlWriter.Create(sb);
            XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(xw);
            sm.XamlWriterMode = XamlWriterMode.Expression;                

            XamlWriter.Save(document, sm );
            return sb.ToString();
        }
    }

    private FlowDocument GetFlowDocument(string xamlText)
    {
        var flowDocument = new FlowDocument();
        if (xamlText != null) flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
        // Set return value
        return flowDocument;
    }

然而,当我试图还原序列化和deserialise下面的代码,我注意到这个不正确的(?)行为

using System;
public class TestCSScript : MarshalByRefObject
{

}

序列化XAML是

using System; public class TestCSScript : MarshalByRefObject {}{ }

通知的新的一组的 “{}”

我在做什么错在这里...在此先感谢您的帮助!

有帮助吗?

解决方案

我已经辞职,为现在迟迟解决方案,但如果有的话,你找到一个干净的,请张贴。

我已经使用的StringBuilder的替换呼叫,以除去不希望的字符。

    private string GetXaml(FlowDocument document)
    {
        if (document == null) return String.Empty;
        else
        {

            StringBuilder sb = new StringBuilder();
            using (XmlWriter xw = XmlWriter.Create(sb))
            {
                XamlDesignerSerializationManager sm = new XamlDesignerSerializationManager(xw);
                sm.XamlWriterMode = XamlWriterMode.Expression;

                XamlWriter.Save(document, sm);
            }
            sb.Replace("{}", "");
            return sb.ToString();
        }

    }

我有一种感觉,当遇到的XamlWriter“{”字符 - 它intreprets,作为一个绑定表达式。我不知道转义序列是干什么用的这个角色。

注 - 我试图改变

XamlWriterMode from XamlWriterMode.Expression to XamlWriterMode.Value

没有喜悦。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top