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

如何在C#中移除XML里名称全大写的属性(含示例)

Remove All Uppercase-Named Attributes from XML in C#

Here's how you can strip out all attributes with fully uppercase names (like AIRPORTNAME in your sample XML) using C#. I'll cover two practical approaches—one with the modern LINQ to XML API, and another with the traditional XmlDocument for older projects.

This method is straightforward and leverages LINQ for clean, readable code:

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // Your raw XML content
        string xmlContent = @"<Flight flightOriginDate=""2018-08-20"" scheduledTimeOfDeparture=""2018-08-20T16:15:00""> 
<FlightIdentification> 
<FLIGHTIDENTIFIGHER> ATS-FPL </FLIGHTIDENTIFIGHER> CLEAR 
<FlightNumber airlineIATACode=""two letter code"" number=""275"" operationalSuffix=""A""> 
<CommercialFlightNumber>275A</CommercialFlightNumber> --""concatnation of IATA Code, Number and operational Suffix"" 
</FlightNumber> 
</FlightIdentification> 
<DepartureAirport AIRPORTNAME=""PORTLAND INTL"" airportFunction=""Departure Airport"" -- HC MASTER > 
<AirportICAOCode>KPWM</AirportICAOCode> 
<AirportIATACode>PWM</AirportIATACode> 
</DepartureAirport> 
<ArrivalAirport airportName=""HEATHROW"" airportFunction="" Arrival Airport"" > 
<AirportICAOCode>EGLL</AirportICAOCode> 
<AirportIATACode>LHR</AirportIATACode> 
</ArrivalAirport> 
</Flight>";

        // Load XML into an XDocument
        XDocument doc = XDocument.Parse(xmlContent);

        // Find all attributes where the name is fully uppercase, then remove them
        doc.Descendants()
           .Attributes()
           .Where(attr => attr.Name.LocalName.All(char.IsUpper))
           .Remove();

        // Output the modified XML
        Console.WriteLine(doc.ToString());
    }
}

How this works:

  • XDocument.Parse() loads your XML string into an in-memory document.
  • Descendants() grabs every element in the XML tree, and Attributes() pulls all attributes from those elements.
  • The Where clause filters attributes whose local name (ignoring any namespace prefixes) consists entirely of uppercase letters.
  • Remove() deletes all matching attributes in one go.

Using XmlDocument (Legacy Approach)

If you're working with an older codebase that uses XmlDocument, this method will get the job done:

using System;
using System.Xml;
using System.Linq;

class Program
{
    static void Main()
    {
        string xmlContent = @"<Flight flightOriginDate=""2018-08-20"" scheduledTimeOfDeparture=""2018-08-20T16:15:00""> 
<FlightIdentification> 
<FLIGHTIDENTIFIGHER> ATS-FPL </FLIGHTIDENTIFIGHER> CLEAR 
<FlightNumber airlineIATACode=""two letter code"" number=""275"" operationalSuffix=""A""> 
<CommercialFlightNumber>275A</CommercialFlightNumber> --""concatnation of IATA Code, Number and operational Suffix"" 
</FlightNumber> 
</FlightIdentification> 
<DepartureAirport AIRPORTNAME=""PORTLAND INTL"" airportFunction=""Departure Airport"" -- HC MASTER > 
<AirportICAOCode>KPWM</AirportICAOCode> 
<AirportIATACode>PWM</AirportIATACode> 
</DepartureAirport> 
<ArrivalAirport airportName=""HEATHROW"" airportFunction="" Arrival Airport"" > 
<AirportICAOCode>EGLL</AirportICAOCode> 
<AirportIATACode>LHR</AirportIATACode> 
</ArrivalAirport> 
</Flight>";

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

        // Get all elements in the XML
        XmlNodeList allNodes = doc.SelectNodes("//*");
        foreach (XmlNode node in allNodes)
        {
            if (node.Attributes != null)
            {
                // Collect attributes to remove first (can't modify collection while iterating)
                var attributesToRemove = node.Attributes.Cast<XmlAttribute>()
                    .Where(attr => attr.Name.All(char.IsUpper))
                    .ToList();

                // Remove each matching attribute
                foreach (XmlAttribute attr in attributesToRemove)
                {
                    node.Attributes.Remove(attr);
                }
            }
        }

        // Output the updated XML
        Console.WriteLine(doc.OuterXml);
    }
}

Key notes for this method:

  • We first collect all attributes to remove into a list because you can't modify the XmlAttributeCollection while iterating over it (it causes an enumeration error).
  • SelectNodes("//*") selects every element in the XML document, ensuring we don't miss any attributes.

Customizing the Filter

If you need to adjust the logic (e.g., remove attributes where all letters are uppercase, even if there are non-letter characters like numbers or underscores), modify the filter condition to:

attr.Name.LocalName.Where(char.IsLetter).All(char.IsUpper)

This will ignore non-letter characters when checking if the name is "effectively" uppercase.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 07:42:28