请尝试以下代码:
private void button1_Click(object sender, EventArgs e)
{
var textConfiguration = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml"));
if (textConfiguration != null)
{
textConfiguration.Descendants("Configuration").Descendants("text").ToList().ForEach(text =>
{
font = text.Attribute("font").Value;
color = text.Attribute("color").Value;
fontsize = text.Attribute("font-size").Value;
textToAppend = text.Value;
});
}
richTextBox1.SelectionColor = Color.FromName(color);
richTextBox1.SelectionFont = new Font(font, int.Parse(fontsize), FontStyle.Regular);
richTextBox1.AppendText(textToAppend);
}
XML文件是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<text font='Verdana' color='Green' font-size='8'>The Formatted Text</text>
</Configuration>
我希望这会给你一个想法。
如果您有更多然后一个文本块,你可以修改为下面的代码:
private void button1_Click(object sender, EventArgs e)
{
var textConfiguration = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml"));
if (textConfiguration != null)
{
textConfiguration.Descendants("Configuration").Descendants("text").ToList().ForEach(text =>
{
font = text.Attribute("font").Value;
color = text.Attribute("color").Value;
fontsize = text.Attribute("font-size").Value;
textToAppend = text.Value;
richTextBox1.SelectionColor = Color.FromName(color);
richTextBox1.SelectionFont = new Font(font, int.Parse(fontsize), FontStyle.Regular);
richTextBox1.AppendText(textToAppend);
});
}
}
和XML会是这样
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<text font='Verdana' color='Green' font-size='8'>The </text>
<text font='Verdana' color='Red' font-size='8'>Formatted </text>
<text font='Verdana' color='Blue' font-size='8'>Text</text>
</Configuration>
我又修改代码,现在你可以在您的XML文件中使用Hex coloe代码。
通过
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml(color);
richTextBox1.SelectionColor = col;
更换
richTextBox1.SelectionColor = Color.FromName(color);
和
color='#ffff80ff'
尝试更换
使用一些所见即所得的编辑器。看到这个链接http://ckeditor.com/ – shajivk 2012-07-30 11:38:00