Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top