问题描述
我正在研究.NET 4.0 Windows应用程序。我使用以下代码将XML文档保存到文件中。
I am working on a .NET 4.0 windows application. I use the following code to save the XML document to a file.
保存XML文件,我可以打开XML文件。但是当我打开文件时,所有节点都显示在一行中。
The XML file is saved and I am able to open the XML file. But when I open the file, the all nodes are shown in a single line.
//形成surdoc XML文档对象并使用XML writer保存它。
//form surdoc XML document object and save it using XML writer.
XmlWriterSettings wSet = new XmlWriterSettings();
//wSet.NewLineChars =" \\\\ n" ;;
wSet.NewLineChars = Environment.NewLine;
wSet.Encoding = Encoding.UTF8;
wSet.OmitXmlDeclaration = true;
wSet.NewLineHandling = NewLineHandling.Replace;
XmlWriter tw = XmlWriter.Create(writepath,wSet);
surdoc.Save(TW);
tw.Flush();
XmlWriterSettings wSet = new XmlWriterSettings();
//wSet.NewLineChars = "\r\n";
wSet.NewLineChars = Environment.NewLine;
wSet.Encoding = Encoding.UTF8;
wSet.OmitXmlDeclaration = true;
wSet.NewLineHandling = NewLineHandling.Replace;
XmlWriter tw = XmlWriter.Create(writepath, wSet);
surdoc.Save(tw);
tw.Flush();
我想在文件中写出这样的输出。
I want to write the output like this in the file.
<节点1>
< ChildN1> test1< / ChildN1>
< ChildN2> test2< / ChildN2>
< / Node1>
<Node1>
<ChildN1>test1</ChildN1>
<ChildN2>test2</ChildN2>
</Node1>
但文件中的输出如下所示。所有节点都在一行中。
But the Output in the file is like below. All nodes are in a single line.
< Node1>< ChildN1> test1< / ChildN1>< ChildN2> test2< / ChildN2>< / Node1>
<Node1><ChildN1>test1</ChildN1><ChildN2>test2</ChildN2></Node1>
如何将XML文档写入XML文件,以便在文件打开时,
子节点显示在下一行?
How to write XMLdocument into a XML file so that when the file is opened
childnodes are displayed in the next line?
谢谢
Ashok
Thanks
Ashok
推荐答案
doc.Save(" file.xml");
doc.Save("file.xml");
如果你想使用XmlWriter并想要缩进,那么显然你应该使用XmlWriterSettings和
http://msdn.microsoft.com/en-us/library/system.xml。设置了xmlwritersettings.indent.aspx :
If you want to use an XmlWriter and want indentation then obviously you should use XmlWriterSettings withhttp://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.indent.aspx being set:
wSet.Indent = true;
wSet.Indent = true;
这篇关于将XML文档写入文件 - 所有节点都显示在一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!