如何将XmlDocument序列化为格式良好的人类可读XML? [英] How to serialize an XmlDocument to a well-formatted human-readable XML?

查看:51
本文介绍了如何将XmlDocument序列化为格式良好的人类可读XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XmlDocument.WriteTo(XmlWriter)方法将 XmlDocument 序列化为文本文件.我也尝试过 XmlDocument.OuterXml .两者都输出单行XML文本,这对于人类来说是很难阅读的(在相当大的XML文档的情况下,这是不可能的).

I use an XmlDocument.WriteTo(XmlWriter) method to serialize an XmlDocument to a text file. I have also tried XmlDocument.OuterXml. Both output a single-line XML text which is very hard (up to impossible in case of a reasonably big XML document) to read for a human.

是否有一种方法可以输出格式良好的人类可读XML文本?我的意思是每个元素都从新行开始,并使用缩进来可视化层次结构级别.

Is there a way to output a well-formatted human-readable XML text instead? I mean with every element starting at a new line and indentation used to visualize hierarchy levels.

推荐答案

我同意BoltClock.这是示例代码-

I agree to BoltClock. Here is the sample code-

//Create a writer to write XML to the console.
XmlTextWriter writer = null;
writer = new XmlTextWriter (Console.Out);
//Use indentation for readability.
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;

这篇关于如何将XmlDocument序列化为格式良好的人类可读XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆