[英]XDocument automatically indents files on parse and does not remove indent on save
I am seeking a help regarding file saving of XML file using XDocument (NOT XMLDocument). 我正在寻求有关使用XDocument(NOT XMLDocument)保存XML文件的帮助。
So I have a xml file that does not have indentation (in fact it is 1 line). 所以我有一个没有缩进的xml文件(实际上是1行)。 When I read this to XDocument using XDocument.Parse (after reading and storing string using
StreamReader
), the resulting XDocument is indented. 当我使用XDocument.Parse将其读取到XDocument时(在使用
StreamReader
读取并存储字符串之后),将缩进所得的XDocument。
Alright, I thought it will be fine as long as if I can save it back to the file without indentation. 好吧,我认为这会很好,只要我可以不缩进地将其保存回文件即可。 However, even though I have
但是,即使我有
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.NewLineOnAttributes = false;
writerSettings.NewLineHandling = NewLineHandling.None;
writerSettings.Indent = false;
and pass that in when I create XmlWriter 并在创建XmlWriter时将其传递给
using (var writer = XmlWriter.Create(u.ToFileSystemPath(), settings))
{
xd.Save(writer);
}
The resulting XML file still has indentation. 生成的XML文件仍然具有缩进。 When I am debugging on Visual studio, I noticed that the
writer
is a class XmlWellFormedWriter
. 在Visual Studio上调试时,我注意到
writer
是XmlWellFormedWriter
类。 Does this have something to do with my result? 这与我的结果有关吗? Any help would be appreciated.
任何帮助,将不胜感激。
Thank you. 谢谢。
SaveOptions are available on Save()
as well as ToString()
. SaveOptions在
Save()
和ToString()
上可用。
string xmlstring =
@"<Top>
<First>1</First>
<Second>Dude</Second>
<Third>Now</Third>
</Top>";
XDocument doc = XDocument.Parse(xmlstring);
doc.Save(@"C:\temp\noIndet.xml", SaveOptions.DisableFormatting);
// string noIndent = doc.ToString(SaveOptions.DisableFormatting);
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.