简体   繁体   English

WPF C#的XML语法突出显示

[英]XML Syntax Highlight for WPF C#

There is an RichTextBox in my application, in which I want to show XML from externhal .xml file. 我的应用程序中有一个RichTextBox,我想在其中显示来自externhal .xml文件的XML。 But I need to show nodes/attributes/Values/Comments everything in xml format color. 但我需要以xml格式颜色显示节点/属性/值/注释。 I have gone through XML Highlight in RichTextBox Link as well. 也在RichTextBox Link中浏览了XML Highlight 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? 那么我可以获得任何API或一些已经构建的代码吗?

I load XML as below 我加载XML如下

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. 编辑:因为我从链接中获取代码并将代码应用于简单的xml。 The result is looks like below 结果如下所示

XML结构

But It should look like 但它应该看起来像

在此输入图像描述

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 代码链接是: http//blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-12-22-80/highlightRTF.txt

You can use the AvalonEdit control 您可以使用AvalonEdit控件

You can get it from nuGet: http://www.nuget.org/packages/AvalonEdit 你可以从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. 请参阅: http//www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor以获取有关如何使用它的指南。

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. 这将为您提供XML语法突出显示并将作为编辑器运行 - 它有点古怪,但我的经验确实很有效。 Just use SyntaxHighlighting="XML" in your XAML. 只需在XAML中使用SyntaxHighlighting="XML"

Its really Simple and Easy. 它真的很简单。

  1. First You need to install AvalonEdit from Manage NuGet Packages. 首先您需要从Manage NuGet Packages安装AvalonEdit。 Use following link to install instructions http://www.nuget.org/packages/AvalonEdit 使用以下链接安装说明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. 或者右键单击解决方案资源管理器中的Project并单击Manage NuGet Packages,现在单击Online和Search Avalon Editor。 Then Install AvalonEditor 然后安装AvalonEditor

  1. Just add following code in xaml file. 只需在xaml文件中添加以下代码即可。

    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit" 的xmlns:avalonedit = “http://icsharpcode.net/sharpdevelop/avalonedit”

  2. Now add your TextEditor as follow 现在添加您的TextEditor如下

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

  3. Now Just load xml file as follow 现在只需加载xml文件,如下所示

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

Thats It :) Hip hip Hurrey :) Thanks Avalon 多数民众赞成:)嘻哈哈里:)谢谢阿瓦隆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM