如何在C#中移除XML里名称全大写的属性(含示例)
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.
Using LINQ to XML (Recommended)
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, andAttributes()pulls all attributes from those elements.- The
Whereclause 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
XmlAttributeCollectionwhile 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

