C# 或 javascript 代码格式化程序

发布于 2024-08-23 10:33:32 字数 452 浏览 15 评论 0原文

我目前正在使用语法荧光笔在页面上显示 XML 或 SOAP 消息。这对于已经正确格式化的消息(换行符、缩进等)来说效果很好。但是,如果我有一个像这样的 XML 字符串:

string xml = "<doc><object><first>Joe</first><last>Smith</last></object></doc>";

我会将字符串写入页面,并且 javascript 荧光笔会正确地语法突出显示该字符串,但它会全部在一行上。

是否有 C# 字符串格式化程序或某些语法突出显示库具有“智能”缩进功能,可以插入换行符、缩进等...?

I'm currently using Syntax Highlighter to show a XML or SOAP messages on a page. That works fine for messages that are already formatted correctly (line breaks, indents, etc). But if I had a XML string like:

string xml = "<doc><object><first>Joe</first><last>Smith</last></object></doc>";

I would write the string to the page and the javascript highlighter would correctly syntax highlight the string, but it would be all on a single line.

Is there a C# string formatter or some syntax highlighting library that has a "smart" indent feature that would insert line breaks, indents, etc... ?

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

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

发布评论

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

评论(1

长伴 2024-08-30 10:33:32

由于这是一个字符串,因此添加换行符和缩进将更改变量 xml 的实际,这不是您想要的代码格式化程序来做!

请注意,您可以在写入页面之前用 C# 格式化 XML,如下所示:

using System;
using System.IO;
using System.Text;
using System.Xml;

namespace XmlIndent
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<doc><object><first>Joe</first><last>Smith</last></object></doc>";
            var xd = new XmlDocument();
            xd.LoadXml(xml);
            Console.WriteLine(FormatXml(xd));
            Console.ReadKey();
        }


        static string FormatXml(XmlDocument doc)
        {
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);
            XmlTextWriter xtw = null;
            using(xtw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
            {
                doc.WriteTo(xtw);
            }
            return sb.ToString();
        }
    }
}

Since this is a string, adding line breaks and indents would be changing the actual value of variable xml, which is not what you want your code formatter to do!

Note that you can format the XML in C# before writing to the page, like this:

using System;
using System.IO;
using System.Text;
using System.Xml;

namespace XmlIndent
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<doc><object><first>Joe</first><last>Smith</last></object></doc>";
            var xd = new XmlDocument();
            xd.LoadXml(xml);
            Console.WriteLine(FormatXml(xd));
            Console.ReadKey();
        }


        static string FormatXml(XmlDocument doc)
        {
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);
            XmlTextWriter xtw = null;
            using(xtw = new XmlTextWriter(sw) { Formatting = Formatting.Indented })
            {
                doc.WriteTo(xtw);
            }
            return sb.ToString();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文