SyntaxHighlighter 不格式化 xml 文本

发布于 2024-11-28 10:08:31 字数 963 浏览 0 评论 0原文

参考文件:

<script src="../../Content/dp.SyntaxHighlighter/Scripts/shCore.js" type="text/javascript"></script>
<script src="../../Content/dp.SyntaxHighlighter/Scripts/shBrushXml.js" type="text/javascript"></script>
<link href="../../Content/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css" rel="stylesheet" type="text/css" />

html代码:

<pre class="brush:xml;">
@Html.Encode("<?xml version='1.0'?><response value='ok' xml:lang='en'>  <text>Ok</text>  <comment html_allowed='true'/>  <ns1:description>  descriptin.  </ns1:description>  <a></a> <a/></response>")
</pre>

JavaScript代码:

<script type="text/javascript">
    SyntaxHighlighter.all()
</script

这里有参考教程

Reference files:

<script src="../../Content/dp.SyntaxHighlighter/Scripts/shCore.js" type="text/javascript"></script>
<script src="../../Content/dp.SyntaxHighlighter/Scripts/shBrushXml.js" type="text/javascript"></script>
<link href="../../Content/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css" rel="stylesheet" type="text/css" />

html Code:

<pre class="brush:xml;">
@Html.Encode("<?xml version='1.0'?><response value='ok' xml:lang='en'>  <text>Ok</text>  <comment html_allowed='true'/>  <ns1:description>  descriptin.  </ns1:description>  <a></a> <a/></response>")
</pre>

JavaScript Code:

<script type="text/javascript">
    SyntaxHighlighter.all()
</script

here is got reference tutorial

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2024-12-05 10:08:31

如果您查看源代码,您会发现您对 XML 进行了两次转义,因为 @ 已经对文本进行了编码,而 @Html.Encode(..)再做一次。因此,您没有获得 SyntaxHighlighter 理解为代码的正确输出。

只需使用此示例进行测试,一切都会正常工作:

<!DOCTYPE html>
<html>
    <head>
        <link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" 
            rel="stylesheet" type="text/css" />
        <link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css"
            rel="stylesheet" type="text/css" />
    </head>
    <body>
        @{
            var xml = @"
                <?xml version='1.0'?>
                <response value='ok' xml:lang='en'>
                    <text>Ok</text>
                    <comment html_allowed='true'/>
                    <ns1:description>  descriptin.  </ns1:description>
                    <a></a>
                    <a/>
                </response>";
        }
        <pre class="brush: xml">
        @xml
        </pre>

        <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js"
            type="text/javascript"></script>
        <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js"
            type="text/javascript"></script>
        <script type="text/javascript">
            SyntaxHighlighter.autoloader(
                    'xml xhtml xslt html            http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js'
                );
            SyntaxHighlighter.all();
        </script>
    </body>
</html>

If you take a look at the source you'll notice that you're excaping the XML two times since @ already encodes the text while @Html.Encode(..) does it again. Therefore you're not getting correct output that SyntaxHighlighter undestand as code.

Just test using this example and everything will work fine:

<!DOCTYPE html>
<html>
    <head>
        <link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" 
            rel="stylesheet" type="text/css" />
        <link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css"
            rel="stylesheet" type="text/css" />
    </head>
    <body>
        @{
            var xml = @"
                <?xml version='1.0'?>
                <response value='ok' xml:lang='en'>
                    <text>Ok</text>
                    <comment html_allowed='true'/>
                    <ns1:description>  descriptin.  </ns1:description>
                    <a></a>
                    <a/>
                </response>";
        }
        <pre class="brush: xml">
        @xml
        </pre>

        <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js"
            type="text/javascript"></script>
        <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js"
            type="text/javascript"></script>
        <script type="text/javascript">
            SyntaxHighlighter.autoloader(
                    'xml xhtml xslt html            http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js'
                );
            SyntaxHighlighter.all();
        </script>
    </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文