WPF C# 的 XML 语法突出显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23084279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:33:05  来源:igfitidea点击:

XML Syntax Highlight for WPF C#

c#xmlwpfsyntaxformatting

提问by VarunJi

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 RichTextBoxLink 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?

我的应用程序中有一个 RichTextBox,我想在其中显示来自 externhal .xml 文件的 XML。但我需要以 xml 格式颜色显示节点/属性/值/评论所有内容。我也经历了RichTextBoxLink 中的XML 突出显示。但它对我没有帮助。由于我完成这项任务的时间较少。那么我可以获得任何 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 linkhave 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 linkand applied the code on a simple xml. The result is looks like below

编辑:因为我从链接中获取了代码并将代码应用于简单的 xml。结果如下所示

XML Structure

XML 结构

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

代码链接是:http: //blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-12-22-80/highlightRTF.txt

回答by Jay

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-Editorfor 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. Just use SyntaxHighlighting="XML"in your XAML.

这将为您提供 XML 语法突出显示,并将用作编辑器 - 设置有点古怪,但根据我的经验确实工作得很好。只需SyntaxHighlighting="XML"在您的 XAML 中使用。

回答by VarunJi

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
  1. 首先,您需要从 Manage NuGet 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. Then Install AvalonEditor

或只需右键单击解决方案资源管理器中的项目,然后单击管理 NuGet 包,现在单击在线和搜索 Avalon 编辑器。然后安装 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");
    
  1. 只需在 xaml 文件中添加以下代码。

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

  2. 现在添加您的文本编辑器如下

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

  3. 现在只需加载 xml 文件如下

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

Thats It :) Hip hip Hurrey :) Thanks Avalon

就是这样 :) Hip hip Hurrey :) 谢谢 Avalon