2012-07-30 88 views
1

我在XML中的文本文件是如何读取文本文件中的XML并将其追加到RichTextBox的

<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追加的内容。即,第一文本标签(the)的内容将设置字体类型“Bamini”,共同LOR是“#ffff80ff”,大小为“8”一样,其他标签也

+0

尝试更换

color='Green' 

使用一些所见即所得的编辑器。看到这个链接http://ckeditor.com/ – shajivk 2012-07-30 11:38:00

回答

0

您应该使用的两个

  1. 一个创建一个有效的XML文档文件:

    CodeProject上试试这个方法你可以储存你的text元素(您当前的格式不是有效的xml)。有关C#Go here中xml操作的更多信息。

  2. 创建一个app.config文件并保存/选择该文件中的值。欲了解更多关于Go here
0

请尝试以下代码:

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' 
+0

我有多个文本如何实现它? – 2012-07-31 06:29:18

+1

@Shankar:参考更新的代码。请接受这个答案,如果它满足您的要求。 – CSharp 2012-07-31 08:11:50

+0

是的,我做到了Nirav我想删除有innerText为空的XML标签? – 2012-07-31 16:01:47

相关问题