我希望将XDocument的默认缩进从2更改为3,但我不太确定如何继续。应该如何完成?
我熟悉XmlTextWriter
,并且已经使用了如下代码:
using System.Xml;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string destinationFile = "C:\myPath\results.xml";
XmlTextWriter writer = new XmlTextWriter(destinationFile, null);
writer.Indentation = 3;
writer.WriteStartDocument();
// Add elements, etc
writer.WriteEndDocument();
writer.Close();
}
}
}
另一个项目中,我使用了 XDocument
,因为它更适合我的类似实现。
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
// Source file has indentation of 3
string sourceFile = @"C:\myPath\source.xml";
string destinationFile = @"C:\myPath\results.xml";
List<XElement> devices = new List<XElement>();
XDocument template = XDocument.Load(sourceFile);
// Add elements, etc
template.Save(destinationFile);
}
}
}
XmlWriter
... - http://msdn.microsoft.com/zh-cn/library/bb336977.aspx - Alexei Levenkov