BACKGROUND
I get a lot of xml files that contain no newlines and to quickly format them I use the function below.
SCENARIO
When I run the tool the first time on a file that contains no newlines
(and no insignificant whitespace) then it works as expected:
Convert("myfile.xml", " ");
如果我对刚刚格式化的同一文件再次运行该工具以增加收件箱,则收件箱不会更改:
Convert("myfile.xml", " ");
QUESTION
Why is the file not formatted the second time I run the function? How do I make sure the function always formats the file?
public static void Convert(string filename, string indent)
{
var input_string = File.ReadAllText(filename, Encoding.UTF8);
var settings = new XmlWriterSettings
{
NewLineHandling = NewLineHandling.Entitize,
Indent = true,
IndentChars = indent,
NewLineChars = Environment.NewLine
};
var sb = new StringBuilder();
using (var reader = XmlReader.Create(new StringReader(input_string)))
using (var writer = XmlWriter.Create(sb, settings))
{
writer.WriteNode(reader, false);
writer.Close();
}
File.Delete(filename);
Encoding utf8 = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
File.WriteAllText(filename, sb.ToString(), utf8);
}
NOTE
If I modify the reader to ignore whitespace then the writer can format the output correctly:
XmlReader.Create(new StringReader(input_string),
new XmlReaderSettings { IgnoreWhitespace = true })
但我仍然想知道为什么当标签之间存在微不足道的空白时,作者未能格式化输出.