Inno Setup:保存带有缩进的XML文档 [英] Inno Setup: Save XML document with indentation
问题描述
我正在尝试在Inno Setup中将新节点添加到XML文件.该节点已正确添加,但下一个标记之前的换行符已删除,或者未添加任何换行符.这是我添加的节点代码:
I'm trying to add a new node to XML file in Inno Setup. The node added correctly but the newline before the next tag is removed or no newline added. Here is my adding node code:
NewNode := XMLDoc.createElement('Test');
XMLDoc.setProperty('SelectionLanguage', 'XPath');
RootNode := XMLDoc.selectSingleNode('//Configuration/AppSettings');
RootNode.appendChild (NewNode);
RootNode.lastChild.text :='New Node';
这是我的XML文件:
<Configuration>
<AppSettings Name="General Settings">
<StartTime/>
<StopTime/>
<TimeBetweenTests>30</TimeBetweenTests>
<Port>600</Port>
<Test>New Node</Test></AppSettings>
</Configuration>
我期望标签
</AppSettings>
保留在添加新节点之前的换行符中.如何添加换行符以保持格式的可读性?
to stay in the newline as it was before the addition of the new node. How could I add a newline to keep the format more readable?
推荐答案
You can use MXXMLWriter
class for the formatting:
procedure SaveXmlDocumentWithIndent(XmlDocument: Variant; FileName: string);
var
Writer: Variant;
Reader: Variant;
FSO: Variant;
TextStream: Variant;
begin
Writer := CreateOleObject('Msxml2.MXXMLWriter');
Reader := CreateOleObject('MSXML2.SAXXMLReader');
FSO := CreateOleObject('Scripting.FileSystemObject');
TextStream := FSO.CreateTextFile(FileName, True);
Writer.Indent := True;
Writer.OmitXMLDeclaration := True;
Reader.ContentHandler := Writer;
Reader.Parse(XmlDocument);
TextStream.Write(Writer.Output);
TextStream.Close();
end;
信用:@cheeso对如何使用缩进来保存MSXML2.DomDocument?(我认为它使用MXXMLWriter).
我刚刚在Pascal Script中重新实现了他的JavaScript代码.
Credits: @cheeso's answer to How can I save an MSXML2.DomDocument with indenting? (I think it uses MXXMLWriter).
I've just re-implemented his JavaScript code in Pascal Script.
上述解决方案将根据 MXXMLWriter
类的要求重新格式化完整的XML文档.
The above solution will reformat complete XML document according to the liking of the MXXMLWriter
class.
如果要保留自己选择的某种格式,则必须通过添加所需的缩进来显式实现它.
If you want to preserve some format of your choice, you have to implement it explicitly by adding the indentation you want.
在添加的节点之后添加新行(#13#10
= CRLF),并使用制表符(#9
),使用:
To add a new line (#13#10
= CRLF) after the added node and (re)indent the closing parent tag with a tab character (#9
), use:
RootNode.appendChild(XMLDoc.createTextNode(#13#10#9));
这篇关于Inno Setup:保存带有缩进的XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!