Tôi đang cố thay đổi thụt lề mặc định của XDocument từ 2 đến 3, nhưng tôi không chắc chắn cách thực hiện. Điều này có thể giải quyết như thế nào?Cách thay đổi số ký tự được sử dụng để thụt đầu dòng khi viết XML với XDocument
Tôi quen thuộc với XmlTextWriter
và đã sử dụng mã như vậy:
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();
}
}
}
Đối với một dự án tôi đã sử dụng XDocument
vì nó hoạt động tốt hơn cho việc thực hiện của tôi tương tự như sau:
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);
}
}
}
'Lưu' nhận' XmlWriter' ... - http://msdn.microsoft.com/en-us/library/bb336977.aspx –