【问题标题】:How can I use AvalonEdit to edit XML files如何使用 AvalonEdit 编辑 XML 文件
【发布时间】:2012-06-28 06:47:29
【问题描述】:

我想在我的应用程序中包含一个 XML 编辑器 - 类似于 VS 的具有自动着色功能的 XML 编辑器等。

AvalonEdit 听起来是个不错的解决方案。

但是,AvalonEdit 附带了一个 C# 语法示例,而不是 XML 语法。是否有 XML 语法的示例?

【问题讨论】:

    标签: wpf xml avalonedit


    【解决方案1】:

    只需在 XAML 中使用 SyntaxHighlighting="XML"

    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
    ...
    <StackPanel>
        <avalonedit:TextEditor SyntaxHighlighting="XML"/>
    </StackPanel>
    

    【讨论】:

    【解决方案2】:

    您所要做的就是更改 Avalon 中的 xshd 文件以将其更改为您需要的语言要求。 SharpDevelop Git 上已经提供了许多常用语言语法高亮功能:Link

    对于 XML,xshd 文件如下所示:

    <SyntaxDefinition name="XML"     extensions=".xml;.xsl;.xslt;.xsd;.manifest;.config;.addin;.xshd;.wxs;.wxi;.wxl;.proj;.csproj;.vbproj;.ilproj;.booproj;.build;.xfrm;.targets;.xaml;.xpt;.xft;.map;.wsdl;.disco;.ps1xml;.nuspec" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
    <Color foreground="Green" name="Comment" exampleText="&lt;!-- comment --&gt;" />
    <Color foreground="Blue" name="CData" exampleText="&lt;![CDATA[data]]&gt;" />
    <Color foreground="Blue" name="DocType" exampleText="&lt;!DOCTYPE rootElement&gt;" />
    <Color foreground="Blue" name="XmlDeclaration" exampleText='&lt;?xml version="1.0"?&gt;' />
    <Color foreground="DarkMagenta" name="XmlTag" exampleText='&lt;tag attribute="value" /&gt;' />
    <Color foreground="Red" name="AttributeName" exampleText='&lt;tag attribute="value" /&gt;' />
    <Color foreground="Blue" name="AttributeValue" exampleText='&lt;tag attribute="value" /&gt;' />
    <Color foreground="Teal" name="Entity" exampleText="index.aspx?a=1&amp;amp;b=2" />
    <Color foreground="Olive" name="BrokenEntity" exampleText="index.aspx?a=1&amp;b=2" />
    
    <RuleSet>
        <Span color="Comment" multiline="true">
            <Begin>&lt;!--</Begin>
            <End>--&gt;</End>
        </Span>
        <Span color="CData" multiline="true">
            <Begin>&lt;!\[CDATA\[</Begin>
            <End>]]&gt;</End>
        </Span>
        <Span color="DocType" multiline="true">
            <Begin>&lt;!DOCTYPE</Begin>
            <End>&gt;</End>
        </Span>
        <Span color="XmlDeclaration" multiline="true">
            <Begin>&lt;\?</Begin>
            <End>\?&gt;</End>
        </Span>
        <Span color="XmlTag" multiline="true">
            <Begin>&lt;</Begin>
            <End>&gt;</End>
            <RuleSet>
                <!-- Treat the position before '<' as end, as that's not a valid character
                     in attribute names and indicates the user forgot a closing quote. -->
                <Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
                    <Begin>"</Begin>
                    <End>"|(?=&lt;)</End>
                </Span>
                <Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
                    <Begin>'</Begin>
                    <End>'|(?=&lt;)</End>
                </Span>
                <Rule color="AttributeName">[\d\w_\-\.]+(?=(\s*=))</Rule>
                <Rule color="AttributeValue">=</Rule>
            </RuleSet>
        </Span>
        <Import ruleSet="EntitySet"/>
    </RuleSet>
    
    <RuleSet name="EntitySet">
        <Rule color="Entity">
            &amp;
            [\w\d\#]+
            ;
        </Rule>
    
        <Rule color="BrokenEntity">
            &amp;
            [\w\d\#]*
            #missing ;
        </Rule>
    </RuleSet>
    </SyntaxDefinition>
    

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多