如何在XML文件中添加换行符?

时间:2022-01-14 07:59:28

I have an XML file and I would like to make a new line in the text "Sample Text 123" like this

我有一个XML文件,我想在文本“Sample text 123”中创建一个新的行。

Sample
Text 123

I've tried already everything I mean &#xA &#xD \n but nothing works:

我已经试过了所有我想说的 \n,但是没有什么管用:

    <?xml version="1.0" encoding="UTF-8" ?>
    <item>
        <text>Address</text>
        <data>
            Sample &#xA; Text 123
        </data>
    </item>

2 个解决方案

#1


14  

A newline (aka line break or end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems:

换行符(即换行符或行尾,EOL)是标记一行文本末尾的特殊字符或字符序列。不同操作系统使用的确切代码不同:

LF:    Unix
CR:    Mac OS up to version 9
CR+LF: Windows, DOS

You can use &#xA; for line feed (LF) or &#xD; for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application. These can be added manually, as you show in your example, but are particularly convenient when needing to add newlines programmatically within a string:

您可以使用& # xA;用于线馈(LF)或 对于回车(CR),当将解析后的文本传递给应用程序时,XML解析器将用相应的字符替换它。这些可以手动添加,如您在示例中所示,但是当需要在字符串中以编程方式添加新行时特别方便:

  • Common programming languages:
    • LF: "&#xA;"
    • 低频:“& # xA;”
    • CR: "&#xD;"
    • 克雷格:“& # xD;”
  • 常用编程语言:LF:“ ”克雷格:“& # xD;”
  • XSLT:
    • LF: <xsl:text>&#xA;</xsl:text>
    • 低频:< xsl:text > & # xA;< / xsl:text >
    • CR: <xsl:text>&#xD;</xsl:text>
    • 克雷格:< xsl:text > & # xD;< / xsl:text >
  • XSLT:低频:< xsl:text > & # xA;< / xsl:text > CR:< xsl:text > & # xD;< / xsl:text >

Or, if you want to see it in the XML immediately, simply put it in literally:

或者,如果您想立即在XML中看到它,只需按字面意思输入:

<?xml version="1.0" encoding="UTF-8" ?>
<item>
    <text>Address</text>
    <data>
        Sample
        Text 123
    </data>
</item>

Newline still not showing up?

Keep in mind that how an application interprets text, including newlines, is up to it. If you find that your newlines are being ignored, it might be that the application automatically runs together text separated by newlines.

请记住,应用程序如何解释文本(包括换行)取决于它。如果您发现您的换行符被忽略了,那么应用程序可能会自动地同时运行由换行符分隔的文本。

HTML browsers, for example, will ignore newlines (and will normalize space within text such that multiple spaces are consolidated). To break lines in HTML,

例如,HTML浏览器将忽略换行(并在文本中规范化空间,以便合并多个空间)。要在HTML中换行,

  • use <br/>; or
  • 使用< br / >;或
  • wrap block in an element such as a div or p which by default causes a line break after the enclosed text, or in an element such as pre which by default typically will preserve whitespace and line breaks; or
  • 在元素(如div或p)中包装块,默认情况下会在所包含的文本之后导致换行,或者在元素(如pre)中包装块,默认情况下通常会保留空格和换行;或
  • use CSS styling such as white-space to control newline rendering.
  • 使用CSS样式(比如空格)来控制换行渲染。

XML application not cooperating?

If an XML application isn't respecting your newlines, and working within the application's processing model isn't helping, another possible recourse is to use CDATA to tell the XML parser not to parse the text containing the newline.

如果XML应用程序不尊重您的换行,并且在应用程序的处理模型中工作不起作用,另一种可能的方法是使用CDATA告诉XML解析器不要解析包含换行的文本。

<?xml version="1.0" encoding="UTF-8" ?>
<item>
    <text>Address</text>
    <data>
        <![CDATA[Sample
                 Text 123]]>
    </data>
</item>

or, if HTML markup is recognized downstream:

或者,如果在下游识别HTML标记:

<?xml version="1.0" encoding="UTF-8" ?>
<item>
    <text>Address</text>
    <data>
        <![CDATA[Sample <br/>
                 Text 123]]>
    </data>
</item>

Whether this helps will depend upon application-defined semantics of one or more stages in the pipeline of XML processing that the XML passes through.

这是否有用将取决于XML处理管道中一个或多个阶段的应用程序定义语义。

Bottom line

A newline (aka line break or end-of-line, EOL) can be added much like any character in XML, but be mindful of

可以像XML中的任何字符一样添加一条换行符(也就是line break或end- line, EOL),但是要注意。

  • differing OS conventions
  • 不同操作系统的约定
  • differing XML application semantics
  • 不同的XML应用程序语义

#2


-1  

See https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlineonattributes(v=vs.110).aspx

见https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlineonattributes(v = vs.110). aspx

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;

#1


14  

A newline (aka line break or end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems:

换行符(即换行符或行尾,EOL)是标记一行文本末尾的特殊字符或字符序列。不同操作系统使用的确切代码不同:

LF:    Unix
CR:    Mac OS up to version 9
CR+LF: Windows, DOS

You can use &#xA; for line feed (LF) or &#xD; for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application. These can be added manually, as you show in your example, but are particularly convenient when needing to add newlines programmatically within a string:

您可以使用& # xA;用于线馈(LF)或 对于回车(CR),当将解析后的文本传递给应用程序时,XML解析器将用相应的字符替换它。这些可以手动添加,如您在示例中所示,但是当需要在字符串中以编程方式添加新行时特别方便:

  • Common programming languages:
    • LF: "&#xA;"
    • 低频:“& # xA;”
    • CR: "&#xD;"
    • 克雷格:“& # xD;”
  • 常用编程语言:LF:“ ”克雷格:“& # xD;”
  • XSLT:
    • LF: <xsl:text>&#xA;</xsl:text>
    • 低频:< xsl:text > & # xA;< / xsl:text >
    • CR: <xsl:text>&#xD;</xsl:text>
    • 克雷格:< xsl:text > & # xD;< / xsl:text >
  • XSLT:低频:< xsl:text > & # xA;< / xsl:text > CR:< xsl:text > & # xD;< / xsl:text >

Or, if you want to see it in the XML immediately, simply put it in literally:

或者,如果您想立即在XML中看到它,只需按字面意思输入:

<?xml version="1.0" encoding="UTF-8" ?>
<item>
    <text>Address</text>
    <data>
        Sample
        Text 123
    </data>
</item>

Newline still not showing up?

Keep in mind that how an application interprets text, including newlines, is up to it. If you find that your newlines are being ignored, it might be that the application automatically runs together text separated by newlines.

请记住,应用程序如何解释文本(包括换行)取决于它。如果您发现您的换行符被忽略了,那么应用程序可能会自动地同时运行由换行符分隔的文本。

HTML browsers, for example, will ignore newlines (and will normalize space within text such that multiple spaces are consolidated). To break lines in HTML,

例如,HTML浏览器将忽略换行(并在文本中规范化空间,以便合并多个空间)。要在HTML中换行,

  • use <br/>; or
  • 使用< br / >;或
  • wrap block in an element such as a div or p which by default causes a line break after the enclosed text, or in an element such as pre which by default typically will preserve whitespace and line breaks; or
  • 在元素(如div或p)中包装块,默认情况下会在所包含的文本之后导致换行,或者在元素(如pre)中包装块,默认情况下通常会保留空格和换行;或
  • use CSS styling such as white-space to control newline rendering.
  • 使用CSS样式(比如空格)来控制换行渲染。

XML application not cooperating?

If an XML application isn't respecting your newlines, and working within the application's processing model isn't helping, another possible recourse is to use CDATA to tell the XML parser not to parse the text containing the newline.

如果XML应用程序不尊重您的换行,并且在应用程序的处理模型中工作不起作用,另一种可能的方法是使用CDATA告诉XML解析器不要解析包含换行的文本。

<?xml version="1.0" encoding="UTF-8" ?>
<item>
    <text>Address</text>
    <data>
        <![CDATA[Sample
                 Text 123]]>
    </data>
</item>

or, if HTML markup is recognized downstream:

或者,如果在下游识别HTML标记:

<?xml version="1.0" encoding="UTF-8" ?>
<item>
    <text>Address</text>
    <data>
        <![CDATA[Sample <br/>
                 Text 123]]>
    </data>
</item>

Whether this helps will depend upon application-defined semantics of one or more stages in the pipeline of XML processing that the XML passes through.

这是否有用将取决于XML处理管道中一个或多个阶段的应用程序定义语义。

Bottom line

A newline (aka line break or end-of-line, EOL) can be added much like any character in XML, but be mindful of

可以像XML中的任何字符一样添加一条换行符(也就是line break或end- line, EOL),但是要注意。

  • differing OS conventions
  • 不同操作系统的约定
  • differing XML application semantics
  • 不同的XML应用程序语义

#2


-1  

See https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlineonattributes(v=vs.110).aspx

见https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlineonattributes(v = vs.110). aspx

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;