替换 XamlPackage 中的文本
我在 RichTextBox 中有一些文本。该文本包含标签,例如:[@TagName!]。我想用数据库中的一些数据替换这些标签,而不丢失格式(字体、颜色、图像等)。我创建了一个方法:
void ReplaceTagsWithData(FlowDocument doc)
{
FileStream fs = new FileStream("tmp.xml", FileMode.Create);
TextRange trTextRange =
new TextRange(doc.ContentStart, doc.ContentEnd);
trTextRange.Save(fs, DataFormats.Xaml);
fs.Dispose();
fs.Close();
StreamReader sr = new StreamReader("tmp.xml");
string rtbContent = sr.ReadToEnd();
MatchCollection mColl =
Regex.Matches(rtbContent,
string.Format(@"\{0}+[a-zA-Z]+{1}",
prefix,
postfix));
foreach (Match m in mColl)
{
string colname =
m.Value.Substring(prefix.Length,
(int)(m.Value.Length - (prefix.Length + postfix.Length)));
rtbContent = rtbContent.Replace(m.Value.ToString(),
dt.Rows[0][colname].ToString());
}
rtbEdit.Document =
new FlowDocument(
(Section)XamlReader.Load(
XmlReader.Create(new StringReader(rtbContent))));
sr.Dispose();
sr.Close();
}
它非常好,但它从内容中删除了图像。我知道我应该使用 XamlPackage 而不是 Xaml,但我无法将其作为纯文本获取。对此还有其他解决方案吗?
感谢您的回答。 ;)
[编辑:13-02-2012 02:14(am)]
我的工作解决方案:
void ReplaceTagsWithData(RichTextBox rtb)
{
FlowDocument doc = rtb.Document;
FileStream fs = new FileStream("tmp", FileMode.Create);
TextRange trTextRange = new TextRange(doc.ContentStart, doc.ContentEnd);
trTextRange.Save(fs, DataFormats.Rtf);
fs.Dispose();
fs.Close();
StreamReader sr = new StreamReader("tmp");
string rtbContent = sr.ReadToEnd();
sr.Dispose();
sr.Close();
MatchCollection mColl =
Regex.Matches(rtbContent,
string.Format(@"\{0}+[a-zA-Z]+{1}",
prefix,
postfix));
foreach (Match m in mColl)
{
string colname =
m.Value.Substring(prefix.Length,
(int)(m.Value.Length - (prefix.Length + postfix.Length)));
rtbContent = rtbContent.Replace(m.Value.ToString(),
dt.Rows[0][colname].ToString());
}
MemoryStream stream =
new MemoryStream(ASCIIEncoding.Default.GetBytes(rtbContent));
rtb.SelectAll();
rtb.Selection.Load(stream, DataFormats.Rtf);
}
也许它不是最好的,但它工作正常。
问题解决了。但我无法发布解决方案,因为它位于我无法再访问的公司服务器上。
I have some text in a RichTextBox. This text includes tags eg: [@TagName!]. I want to replace these tags with some data from a database without losing formatting (fonts, colors, image, etc). I've created a method:
void ReplaceTagsWithData(FlowDocument doc)
{
FileStream fs = new FileStream("tmp.xml", FileMode.Create);
TextRange trTextRange =
new TextRange(doc.ContentStart, doc.ContentEnd);
trTextRange.Save(fs, DataFormats.Xaml);
fs.Dispose();
fs.Close();
StreamReader sr = new StreamReader("tmp.xml");
string rtbContent = sr.ReadToEnd();
MatchCollection mColl =
Regex.Matches(rtbContent,
string.Format(@"\{0}+[a-zA-Z]+{1}",
prefix,
postfix));
foreach (Match m in mColl)
{
string colname =
m.Value.Substring(prefix.Length,
(int)(m.Value.Length - (prefix.Length + postfix.Length)));
rtbContent = rtbContent.Replace(m.Value.ToString(),
dt.Rows[0][colname].ToString());
}
rtbEdit.Document =
new FlowDocument(
(Section)XamlReader.Load(
XmlReader.Create(new StringReader(rtbContent))));
sr.Dispose();
sr.Close();
}
It's quite good but it removes images from content. I know that I should use XamlPackage instead of Xaml but then I can't get it as a plain text. Is there any other solution for this?
Thanks for answers. ;)
[EDIT: 13-02-2012 02:14(am)]
My working solution:
void ReplaceTagsWithData(RichTextBox rtb)
{
FlowDocument doc = rtb.Document;
FileStream fs = new FileStream("tmp", FileMode.Create);
TextRange trTextRange = new TextRange(doc.ContentStart, doc.ContentEnd);
trTextRange.Save(fs, DataFormats.Rtf);
fs.Dispose();
fs.Close();
StreamReader sr = new StreamReader("tmp");
string rtbContent = sr.ReadToEnd();
sr.Dispose();
sr.Close();
MatchCollection mColl =
Regex.Matches(rtbContent,
string.Format(@"\{0}+[a-zA-Z]+{1}",
prefix,
postfix));
foreach (Match m in mColl)
{
string colname =
m.Value.Substring(prefix.Length,
(int)(m.Value.Length - (prefix.Length + postfix.Length)));
rtbContent = rtbContent.Replace(m.Value.ToString(),
dt.Rows[0][colname].ToString());
}
MemoryStream stream =
new MemoryStream(ASCIIEncoding.Default.GetBytes(rtbContent));
rtb.SelectAll();
rtb.Selection.Load(stream, DataFormats.Rtf);
}
Maybe it's not the best but it's working correctly.
It was solved. But I can't post solution because it is on company server which I can't access anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Razor 引擎在模板主题中执行您想要的任何操作。您可以从 nuget (http://www.nuget.org/packages/RazorEngine) 下载无需任何设置配置,您可以使用 Razor 语法来执行此操作。例如,您的模板可以是这样的:
注意:来自 Razor 的 @Model.Title 和 @Model.Icon
实际上,我使用 RazorEngine 来完成所有模板任务:
电子邮件、动态生成报告 (rdlc) 等...
You can use the Razor Engine to do whatever you want in the templating subject. You can download from nuget (http://www.nuget.org/packages/RazorEngine) and without any setup configuration you can use the Razor syntax to do this. For example your template can be this:
Note: the @Model.Title and @Model.Icon thats come from Razor
Actually i use RazorEngine to all my templating task:
Email, Report generation on the fly (rdlc), etc...
您可以使用Aspose.dll。
它有完整的论坛/示例和文档
使用 aspose.dll 替换基于正则表达式的文本< /a>
You can use Aspose.dll.
it has full forum / examples and documents
Replace Text Based on Regular Expression with aspose.dll
您使用的正则表达式是贪婪的,因此会匹配从一个标记的开头到下一个标记的结尾的所有内容。将其更改为
@"\{0}[a-zA-Z]+?{1}"
以获得更好的匹配效果。此外,使用带有 lambda 的 Regex.Replace 重载将会使代码更简洁。
The Regex you are using is greedy so would match everything from the start of one token to the end of a following one. Change it to
@"\{0}[a-zA-Z]+?{1}"
for a better match.Also, using the overload of Regex.Replace that takes a lambda would be cleaner code.
尝试使用 Regex.Replace 方法。您可以在MSDN中找到该方法的参考
http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Try using Regex.Replace Method. You can find reference to the method in MSDN
http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx