Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have the xml in a text file is
XML
<?xml version="1.0" encoding="utf-8" ?> 
- <TXT>
  <text font="Microsoft Sans Serif" color="#FF8000" font-size="16">testing</text> 
  <text font="Kal-72" color="#FFFF00" font-size="26">sample</text> 
  <text font="Arial Black" color="#8080FF" font-size="26">text</text> 
  </TXT>


I would like to append the content of the text tag to the richtextbox. i.e., the text tag element string "testing sample text" is to be append to the richtextbox with their corresponding font type,size and color properties in text tag..
Posted
Updated 30-Jul-12 19:57pm
v2
Comments
Sergey Alexandrovich Kryukov 30-Jul-12 12:24pm    
Not clear, what's the problem? What did you try so far? You have .NET XML parsers, you have RichTextBox (what's it the exact type? there are more then one under this name...), fully documented. So, what kind if help do you need?
--SA
NeonMika 30-Jul-12 16:35pm    
Yeah, it's not sure. If you have problems working with XML try to search for some infos on XmlDocument.
As far as I know, you can work with some kind of selection on your RichtextBox. So you append your text to the box, then you select it via code and format the selected text.

You can use LINQ to XML ( which is best ) or the XmlDocument class ( which requires more code ) to get the value out of your XML.

Here[^]

My guess:

XML
IEnumerable<string> text =
    from item in doc.Descendants("text")
    select (string) item.Attribute("PartNumber");


I am not sure if you need to reference the top level element or not. Of course you need to load the XDocument with your file first.
 
Share this answer
 
First convert the text to xml file and do the following code

C#
 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); 
}); 
} 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900