热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

缩进XML,但保持其他所有内容不变-IndentingXML,butleavingeverythingelseuntouched

Myfirstquestionhere,sobearwithme.Basically,myproblemisthis:ImbuildinganXMLIDEfora

My first question here, so bear with me. Basically, my problem is this: Im building an XML IDE for an internal language. A feature of it should be to auto-indent the XML by using some command. Similar to what is found in Visual Studio etc.

我的第一个问题,请耐心等待。基本上,我的问题是:我正在为内部语言构建XML IDE。它的一个特性应该是使用一些命令自动缩进XML。与Visual Studio等中的内容类似。

Basically what I need is to turn the following Xml:

基本上我需要的是打开以下Xml:



        Tove

    Jani
        Reminder
    Don't forget me this weekend!


Into:



    Tove

    Jani
    Reminder
    Don't forget me this weekend!


That is indent - but touch nothing else. Is this possible in C# without writing an algorithm from scratch, i.e., with LINQ XDocument or some XmlWriter implementaion?

这是缩进 - 但没有别碰。这是否可以在C#中从头编写算法,即使用LINQ XDocument或某些XmlWriter实现?

I've tried the following so far (from What is the simplest way to get indented XML with line breaks from XmlDocument?)

到目前为止,我已经尝试过以下内容(从XmlDocument中使用换行符获取缩进XML的最简单方法是什么?)

static public string Beautify(this XmlDocument doc)
{
    StringBuilder sb = new StringBuilder();
    XmlWriterSettings settings = new XmlWriterSettings
    {
        Indent = true,
        IndentChars = "  ",
        NewLineChars = "\r\n",
        NewLineHandling = NewLineHandling.Replace
    };
    using (XmlWriter writer = XmlWriter.Create(sb, settings)) {
        doc.Save(writer);
    }
    return sb.ToString(); 
}

But this removes linebreaks and gives me:

但这会删除换行符并给我:



    Tove
    Jani
    Reminder
    Don't forget me this weekend!

Thanks in advance to anyone with comments or answers.

提前感谢任何有意见或回答的人。

3 个解决方案

#1


4  

I'd try replacing all newlines with a custom tag (e.g. , running the result through your existing Beautify code and then replacing the newline tags with proper newlines again.

我尝试使用自定义标记替换所有换行符(例如 ,通过现有的Beautify代码运行结果,然后再次使用正确的换行替换换行符。

UPDATE: Thinking about this more you might need to replace \n\n with '\n', but you get the general idea.

更新:考虑到这一点你可能需要用'\ n'替换\ n \ n,但你得到了一般的想法。

#2


2  

Building on Mark's good suggestion, this will beautify the XML string (but the code isn't very pretty):

基于Mark的好建议,这将美化XML字符串(但代码不是很漂亮):

class Program
{
    static void Main(string[] args)
    {
        string test = @"

    Tove

Jani
    Reminder
Don't forget me this weekend!

";
        string output = Test.BeautifyXML(test);
        Console.Write(output);
        Console.ReadLine();
    }
}
static class Test { 
    static public string BeautifyXML(this string docString)
    {
        docString = Regex.Replace(docString.Replace("\r", "").Replace("\n", ""),@"\?>()*", "?>");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(docString);
        StringBuilder sb = new StringBuilder();
        XmlWriterSettings settings = new XmlWriterSettings
        {
            Indent = true,
            IndentChars = "  ",
            NewLineChars = "\r\n",
            NewLineHandling = NewLineHandling.Replace
        };
        using (XmlWriter writer = XmlWriter.Create(sb, settings))
        {
            doc.Save(writer);
        }
        return Regex.Replace(sb.ToString().Replace("\r\n", ""), @"( )*", "\r\n").Replace("?>", "?>\r\n");
    }
}

Output:



  Tove

  Jani
  Reminder
  Don't forget me this weekend!


#3


0  

This might do the trick for you

这可能对你有所帮助

instead of

NewLineHandling = NewLineHandling.Replace

use

NewLineHandling = NewLineHandling.None

The None setting tells the XmlWriter to leave the input unchanged. This setting is used when you not want any new-line processing.

None设置告诉XmlWriter保持输入不变。当您不需要任何换行处理时,将使用此设置。


推荐阅读
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
author-avatar
迷失刀
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有