XDocument 在生成最终 xml 字符串时添加回车符 return
XDocument adds carriage return when generating final xml string
我有一个案例,我想在 post 将其转换为 API 之前生成 xml,其中包含换行符 (\n) 但不是回车 returns (没有 \r).
虽然在 C# 中,XDocument 似乎会自动在其 to-string 方法中添加回车符 returns:
var inputXmlString = "<root>Some text without carriage return\nthis is the new line</root>";
// inputXmlString: <root>Some text without carriage return\nthis is the new line</root>
var doc = XDocument.Parse(inputXmlString);
var xmlString = doc.Root.ToString();
// xmlString: <root>Some text without carriage return\n\rthis is the new line</root>
在 doc.Root.ToString() 中,在元素之间添加了一组 \n\r 以进行缩进,这对于接收者对整个 xml 消息的解释无关紧要。但是,ToString() 方法还在我需要保留独立换行符的实际文本字段中添加了 \r(\n 之后没有 \r)。
我知道我可以进行最终字符串替换,在执行实际 HTTP post 之前从最终字符串中删除所有回车符 returns,但这似乎是错误的。
使用 XElement 对象而不是 Document.Parse 构建 xml 文档时,问题相同。即使我使用 CData 元素来包装文本,问题仍然存在。
任何人都可以向我解释,如果我做错了什么,或者是否有一些干净的方法来实现我尝试做的事情?
你试过了吗:
how to remove carriage returns, newlines, spaces from a string
string result = XElement.Parse(input).ToString(SaveOptions.DisableFormatting);
Console.WriteLine(result);
XNode.ToString
是在幕后使用 XmlWriter
的便利 - 您可以在 reference source.
中查看代码
每 the documentation 对于 XmlWriterSettings.NewLineHandling
:
The Replace setting tells the XmlWriter to replace new line characters with \r\n, which is the new line format used by the Microsoft Windows operating system. This helps to ensure that the file can be correctly displayed by the Notepad or Microsoft Word applications. This setting also replaces new lines in attributes with character entities to preserve the characters. This is the default value.
这就是为什么当您将元素转换回字符串时会看到这一点。如果你想改变这种行为,你必须用你自己的 XmlWriterSettings
:
创建你自己的 XmlWriter
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
NewLineHandling = NewLineHandling.None
};
string xmlString;
using (var sw = new StringWriter())
{
using (var xw = XmlWriter.Create(sw, settings))
{
doc.Root.WriteTo(xw);
}
xmlString = sw.ToString();
}
其他答案对我不起作用(在我将其转换为 VB 之后)
但这确实发生了:
Return xDoc.ToString(SaveOptions.DisableFormatting)
我有一个案例,我想在 post 将其转换为 API 之前生成 xml,其中包含换行符 (\n) 但不是回车 returns (没有 \r).
虽然在 C# 中,XDocument 似乎会自动在其 to-string 方法中添加回车符 returns:
var inputXmlString = "<root>Some text without carriage return\nthis is the new line</root>";
// inputXmlString: <root>Some text without carriage return\nthis is the new line</root>
var doc = XDocument.Parse(inputXmlString);
var xmlString = doc.Root.ToString();
// xmlString: <root>Some text without carriage return\n\rthis is the new line</root>
在 doc.Root.ToString() 中,在元素之间添加了一组 \n\r 以进行缩进,这对于接收者对整个 xml 消息的解释无关紧要。但是,ToString() 方法还在我需要保留独立换行符的实际文本字段中添加了 \r(\n 之后没有 \r)。
我知道我可以进行最终字符串替换,在执行实际 HTTP post 之前从最终字符串中删除所有回车符 returns,但这似乎是错误的。
使用 XElement 对象而不是 Document.Parse 构建 xml 文档时,问题相同。即使我使用 CData 元素来包装文本,问题仍然存在。
任何人都可以向我解释,如果我做错了什么,或者是否有一些干净的方法来实现我尝试做的事情?
你试过了吗:
how to remove carriage returns, newlines, spaces from a string
string result = XElement.Parse(input).ToString(SaveOptions.DisableFormatting);
Console.WriteLine(result);
XNode.ToString
是在幕后使用 XmlWriter
的便利 - 您可以在 reference source.
每 the documentation 对于 XmlWriterSettings.NewLineHandling
:
The Replace setting tells the XmlWriter to replace new line characters with \r\n, which is the new line format used by the Microsoft Windows operating system. This helps to ensure that the file can be correctly displayed by the Notepad or Microsoft Word applications. This setting also replaces new lines in attributes with character entities to preserve the characters. This is the default value.
这就是为什么当您将元素转换回字符串时会看到这一点。如果你想改变这种行为,你必须用你自己的 XmlWriterSettings
:
XmlWriter
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
NewLineHandling = NewLineHandling.None
};
string xmlString;
using (var sw = new StringWriter())
{
using (var xw = XmlWriter.Create(sw, settings))
{
doc.Root.WriteTo(xw);
}
xmlString = sw.ToString();
}
其他答案对我不起作用(在我将其转换为 VB 之后)
但这确实发生了:
Return xDoc.ToString(SaveOptions.DisableFormatting)