我想從2改變的XDocument的默認縮進至3編寫XML時用於縮進字符數,但我不太清楚如何進行。如何才能做到這一點?如何改變用的XDocument
我熟悉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/en-us/library/bb336977.aspx –