How can I use AvalonEdit to edit XML files

拥有回忆 提交于 2019-12-23 21:52:05

问题


I would like to include an XML editor in my app - similar to VS's XML editor with auto-coloring, etc.

AvalonEdit sounds like a great solution.

However, AvalonEdit comes with a sample for C# syntax, not XML syntax. Is there a sample for XML syntax somewhere?


回答1:


Just use SyntaxHighlighting="XML" in your XAML:

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



回答2:


All you have to do is change the xshd file in Avalon to change it to the language requirements you need. Many of the common language syntax highlighting is already provided on the SharpDevelop Git: Link

For XML, the xshd file looks like:

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


来源:https://stackoverflow.com/questions/11216008/how-can-i-use-avalonedit-to-edit-xml-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!