You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中移除XML首行后的一个制表符以规范格式

C# 移除XML首行后的多余缩进

问题场景

原始XML字符串:

<?xml version="1.0" encoding="utf-16"?>
    <reportResponse>
      <report>
        <inquiry>
          <agencyId>32</agencyId>
          <archiveId>4055062521111790</archiveId>
          <originalArchiveId>0</originalArchiveId>
          <sequenceNr>0</sequenceNr>
        </inquiry>
      </report>
    </reportResponse>

需要调整为层级规范的格式,移除首行后所有行开头的4个空格(对应一个制表符的缩进量):

<?xml version="1.0" encoding="utf-16"?>
<reportResponse>
  <report>
    <inquiry>
      <agencyId>32</agencyId>
      <archiveId>4055062521111790</archiveId>
      <originalArchiveId>0</originalArchiveId>
      <sequenceNr>0</sequenceNr>
    </inquiry>
  </report>
</reportResponse>

实现方案

方案1:逐行处理字符串

直接拆分字符串为行,首行保留,后续行移除开头的指定缩进:

string originalXml = @"<?xml version=""1.0"" encoding=""utf-16""?>
    <reportResponse>
      <report>
        <inquiry>
          <agencyId>32</agencyId>
          <archiveId>4055062521111790</archiveId>
          <originalArchiveId>0</originalArchiveId>
          <sequenceNr>0</sequenceNr>
        </inquiry>
      </report>
    </reportResponse>";

// 按系统换行符拆分,保留所有行
string[] lines = originalXml.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

// 从第二行开始,移除开头的4个空格
for (int i = 1; i < lines.Length; i++)
{
    if (lines[i].StartsWith("    "))
    {
        lines[i] = lines[i].Substring(4);
    }
}

string formattedXml = string.Join(Environment.NewLine, lines);

方案2:正则表达式批量替换

用正则匹配首行后的行开头缩进,一次性替换:

using System.Text.RegularExpressions;

string originalXml = @"<?xml version=""1.0"" encoding=""utf-16""?>
    <reportResponse>
      <report>
        <inquiry>
          <agencyId>32</agencyId>
          <archiveId>4055062521111790</archiveId>
          <originalArchiveId>0</originalArchiveId>
          <sequenceNr>0</sequenceNr>
        </inquiry>
      </report>
    </reportResponse>";

// 匹配首行之外的所有行开头的4个空格,替换为空
string formattedXml = Regex.Replace(originalXml, @"^(?<!\A)    ", "", RegexOptions.Multiline);

方案3:XML解析器重新格式化(推荐)

如果XML结构复杂,用官方XML解析器处理更稳妥,能自动生成规范缩进:

using System.Xml;
using System.Text;

string originalXml = @"<?xml version=""1.0"" encoding=""utf-16""?>
    <reportResponse>
      <report>
        <inquiry>
          <agencyId>32</agencyId>
          <archiveId>4055062521111790</archiveId>
          <originalArchiveId>0</originalArchiveId>
          <sequenceNr>0</sequenceNr>
        </inquiry>
      </report>
    </reportResponse>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(originalXml);

XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true,
    IndentChars = "  ", // 用两个空格作为缩进单位
    OmitXmlDeclaration = false,
    Encoding = Encoding.Unicode // 对应utf-16编码
};

using (StringWriter sw = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(sw, settings))
{
    doc.WriteTo(writer);
    writer.Flush();
    string formattedXml = sw.ToString();
}

内容的提问来源于stack exchange,提问作者Sajeel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 18:05:27