简体   繁体   中英

XML attribute finding

<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>

How to remove attribute which are in all letters capital letters in c# from an xml. For ex- FLIGHTIDENTIFIGHER, AIRPORTNAME in above xml

First you have to add necessary namespaces:

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

Then you load your sample XML into XDocument.

var xDocument = XDocument.Load("XmlPath.xml");

And now with the help of LINQ, you delete elements and attributes which names are uppercase.

xDocument.Root.Descendants().Where(elem => elem.Name.LocalName.ToString()
                            .All(char.IsUpper))
                            .Remove();

xDocument.Root.Descendants().Attributes()
                            .Where(elem => elem.Name.ToString()
                            .All(char.IsUpper))
                            .Remove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM