小编Chr*_*ber的帖子

使用XDocument编写XML时如何更改用于缩进的字符数

我试图将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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于我使用的另一个项目,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> …
Run Code Online (Sandbox Code Playgroud)

c# xml linq-to-xml

15
推荐指数
1
解决办法
2893
查看次数

标签 统计

c# ×1

linq-to-xml ×1

xml ×1