4

There is an RichTextBox in my application, in which I want to show XML from externhal .xml file. But I need to show nodes/attributes/Values/Comments everything in xml format color. I have gone through XML Highlight in RichTextBox Link as well. But Its not helpful for me. As I have less time to complete this task. So can I get any API or some already built code for this?

I load XML as below

XmlDocument doc = new XmlDocument();
doc.Load("filepath.xml");
gameListXMLRichText.Document.Blocks.Clear();
gameListXMLRichText.AppendText(doc.InnerXml.ToString());

Formatting from the above link have lots of issues. but I can't go through that as of now, because of time. So please help me on this. Thanks in Advance.

Edit: As I have taken code from the link and applied the code on a simple xml. The result is looks like below

XML Structure

But It should look like

enter image description here

Only Proper color is required, for me. Proper formatting is not required.

Code link is : http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-12-22-80/highlightRTF.txt

VarunJi
  • 685
  • 2
  • 14
  • 31
  • If you could post what result you are getting and what results you consider as acceptable would help us solve/help with the issue, otherwise we can't guess. – XAMlMAX Apr 15 '14 at 12:57
  • i have edited my Question, please check. – VarunJi Apr 15 '14 at 13:22

2 Answers2

13

You can use the AvalonEdit control

You can get it from nuGet: http://www.nuget.org/packages/AvalonEdit

See: http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor for a guide on how to use it.

This will give you XML syntax highlighting and will function as an editor - it's a bit quirky to set up but does work quite well in my experience. Just use SyntaxHighlighting="XML" in your XAML.

Jay
  • 9,561
  • 7
  • 51
  • 72
  • Supab... Its a simple application. Just need to follow the instruction 1. Install Avalon http://www.nuget.org/packages/AvalonEdit 2. Use like xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit" ... – VarunJi Apr 15 '14 at 15:03
  • http://stackoverflow.com/questions/11216008/how-can-i-use-avalonedit-to-edit-xml-files – VarunJi Apr 15 '14 at 15:03
  • 1
    I'm glad this helped you. I said it was quirky but I didn't say why; I couldn't work out how to data bind this control to a view object. When I last used it (which was over a year ago now!), I did exactly what you did which was to set the text property programmatically. Good luck, Jay. – Jay Apr 21 '14 at 18:22
3

Its really Simple and Easy.

  1. First You need to install AvalonEdit from Manage NuGet Packages. Use following link to install instructions http://www.nuget.org/packages/AvalonEdit

or Just right click on Project in Solution Explorer and Click Manage NuGet Packages, now click Online and Search Avalon Editor. Then Install AvalonEditor

  1. Just add following code in xaml file.

    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"

  2. Now add your TextEditor as follow

    <avalonedit:TextEditor SyntaxHighlighting="XML" x:Name="gameListXMLText" Height="200">

  3. Now Just load xml file as follow

    gameListXMLText.Text =  File.ReadAllText("sample.xml");
    

Thats It :) Hip hip Hurrey :) Thanks Avalon

VarunJi
  • 685
  • 2
  • 14
  • 31