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

如何使用C#替换XML属性值的前缀?

C# 修改XML特定ID属性的实现方案

针对需求,这里提供两种常用的C#实现方式,分别基于Linq to XML(XDocument)和传统XmlDocument,均可完成指定的ID修改操作:

方法一:使用Linq to XML(推荐,代码更简洁)

Linq to XML是.NET中现代的XML处理API,语法简洁易维护:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // 原始XML内容
        string xmlContent = @"<ce_table frame=""topbot"" id=""t0010"" rowsep=""0"" colsep=""0"">   
    <ce_label>Table 2</ce_label> 
        <ce_caption id=""cn040""> 
            <ce_simple-para id=""spar055"">Model fit cnbs for the span targeted moments.</ce_simple-para> 
        </ce_caption>
    </ce_label>  
</ce_table>";

        // 加载XML文档
        XDocument doc = XDocument.Parse(xmlContent);

        // 修改ce_table的id:t0010 → tf0010
        var ceTableElement = doc.Element("ce_table");
        if (ceTableElement != null)
        {
            var idAttr = ceTableElement.Attribute("id");
            if (idAttr != null && idAttr.Value == "t0010")
            {
                idAttr.Value = "tf0010";
            }
        }

        // 遍历所有元素,将以"cn"开头的id前缀替换为"cib"
        foreach (var element in doc.Descendants())
        {
            var idAttr = element.Attribute("id");
            if (idAttr != null && idAttr.Value.StartsWith("cn", StringComparison.Ordinal))
            {
                // 保留原后缀,替换前缀
                idAttr.Value = $"cib{idAttr.Value.Substring(2)}";
            }
        }

        // 输出修改后的XML
        string modifiedXml = doc.ToString();
        Console.WriteLine(modifiedXml);
    }
}

方法二:使用传统XmlDocument

若需兼容旧版本.NET框架,或更熟悉DOM操作,可使用XmlDocument:

using System;
using System.Xml;

class Program
{
    static void Main()
    {
        // 原始XML内容
        string xmlContent = @"<ce_table frame=""topbot"" id=""t0010"" rowsep=""0"" colsep=""0"">   
    <ce_label>Table 2</ce_label> 
        <ce_caption id=""cn040""> 
            <ce_simple-para id=""spar055"">Model fit cnbs for the span targeted moments.</ce_simple-para> 
        </ce_caption>
    </ce_label>  
</ce_table>";

        // 加载XML文档
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlContent);

        // 修改ce_table的id:t0010 → tf0010
        XmlNode ceTableNode = doc.SelectSingleNode("//ce_table");
        if (ceTableNode != null)
        {
            XmlAttribute idAttr = ceTableNode.Attributes["id"];
            if (idAttr != null && idAttr.Value == "t0010")
            {
                idAttr.Value = "tf0010";
            }
        }

        // 遍历所有带id属性的元素,替换"cn"前缀为"cib"
        XmlNodeList elementsWithId = doc.SelectNodes("//*[@id]");
        foreach (XmlNode node in elementsWithId)
        {
            XmlAttribute idAttr = node.Attributes["id"];
            if (idAttr != null && idAttr.Value.StartsWith("cn", StringComparison.Ordinal))
            {
                idAttr.Value = $"cib{idAttr.Value.Substring(2)}";
            }
        }

        // 输出修改后的XML
        string modifiedXml = doc.OuterXml;
        Console.WriteLine(modifiedXml);
    }
}

关键说明

  • 前缀替换逻辑:对以"cn"开头的ID,通过Substring(2)截取后缀部分,再拼接"cib"前缀,确保仅修改前缀不改动后续编号。
  • 大小写控制:代码使用StringComparison.Ordinal做大小写敏感匹配,若需忽略大小写,可替换为StringComparison.OrdinalIgnoreCase。
  • 文件加载:若XML来自文件,将Parse/LoadXml替换为Load("文件路径")即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 15:40:48