2012-07-30 85 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

相關問題