C# 如何读取文本文件中的XML并将其附加到RichTextBox
我有一个文本文件中的xml是C# 如何读取文本文件中的XML并将其附加到RichTextBox,c#,C#,我有一个文本文件中的xml是 <text font='Bamini' color='#ffff80ff' font-size='8'>the </text> <text font='Microsoft Sans Serif' color='#ff804000' font-size='8'>test </text> <text font='Microsoft Sans Serif' color='#ff8000ff' font-size='8'
<text font='Bamini' color='#ffff80ff' font-size='8'>the </text>
<text font='Microsoft Sans Serif' color='#ff804000' font-size='8'>test </text>
<text font='Microsoft Sans Serif' color='#ff8000ff' font-size='8'>text </text>
<text font='Kal-72' color='#ff0080c0' font-size='8'>sample</text>
我想将文本标记的内容附加到RichTextBox。i、 例如,第一个文本标记的内容将设置字体类型Bamini,颜色为ffff80ff,大小为“8”,与其他标记类似,您也可以使用这些标记将RTF格式的文本放入控件中进行显示,或者使用控件文本中定义的指定RTF格式提取控件的文本
RTF编码不同于HTML。你不能马上这么做。我建议使用浏览器控制
或者从codeproject中尝试以下方法:
可以使用将RTF格式的文本放置到控件中以供显示,或使用控件文本中定义的指定RTF格式提取控件文本
RTF编码不同于HTML。你不能马上这么做。我建议使用浏览器控制
或者从codeproject中尝试以下方法:
你应该使用这两种方法中的一种 创建一个有效的xml文档文件,您可以在其中存储文本元素您当前的格式不是有效的xml。有关C中xml操作的更多信息。 创建app.config文件并保存/选择该文件中的值。更多关于这方面的信息。
你应该使用这两种方法中的一种 创建一个有效的xml文档文件,您可以在其中存储文本元素您当前的格式不是有效的xml。有关C中xml操作的更多信息。 创建app.config文件并保存/选择该文件中的值。更多关于这方面的信息。
请尝试以下代码:
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>
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>
借
替换
color='Green'
借
请尝试以下代码:
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>
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>
借
替换
color='Green'
借
尝试使用所见即所得编辑器。请参阅此链接,尝试使用所见即所得编辑器。看到这个链接了吗?我有不止一个文本如何实现它?@Shankar:参考更新的代码。如果满足您的要求,请接受这个答案。是的,我做到了。我想删除xml标记,其中innerText为空?Shankar,首先,删除xml标记是什么意思?如果innerText为空,即Formatted被替换为,则上述代码将在richTextBox1中显示格式化文本。这相当于删除文本元素。我有不止一个文本如何实现它?@Shankar:参考更新的代码。如果满足您的要求,请接受这个答案。是的,我做到了。我想删除xml标记,其中innerText为空?Shankar,首先,删除xml标记是什么意思?如果innerText为空,即Formatted被替换为,则上述代码将在richTextBox1中显示格式化文本。这相当于删除文本元素。