作者:迷失刀 | 来源:互联网 | 2023-05-18 00:21
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 个解决方案