简体   繁体   中英

C# deserialize specific xml elements into custom list

I have an issue where I'm trying to put all the sales periods(SalesYear, SalesToday...) XML elements into a custom C# list where all the sales periods should be items.

This is the returned XML I'm deserializing:

<SalesStatisticsPartner>
    <PartnerId>137</PartnerId>
    <PartnerName>Humac DK</PartnerName>
    <LocalCurrency>DKK</LocalCurrency>
    <SalesYear>1252</SalesYear>
    <SalesYearPremium>1861548.0</SalesYearPremium>
    <SalesLastMonth>1241</SalesLastMonth>
    <SalesLastMonthPremium>1852359.0</SalesLastMonthPremium>
    <SalesMonth>11</SalesMonth>
    <SalesMonthPremium>9189.0</SalesMonthPremium>
    <SalesLastWeek>0</SalesLastWeek>
    <SalesLastWeekPremium>0.0</SalesLastWeekPremium>
    <SalesWeek>0</SalesWeek>
    <SalesWeekPremium>0.0</SalesWeekPremium>
    <SalesYesterday>0</SalesYesterday>
    <SalesYesterdayPremium>0.0</SalesYesterdayPremium>
    <SalesToday>0</SalesToday>
    <SalesTodayPremium>0.0</SalesTodayPremium>
</SalesStatisticsPartner>

This is my current C# class which I'm deserializing into:

[XmlRoot(ElementName = "SalesStatisticsPartner"]
    public class SalesStatisticsPartner
    {   
        public int PartnerId { get; set; }

        public string PartnerName { get; set; }

        public List<SalesPeriod> SalesPeriods { get; set; }

        public string LocalCurrency { get; set; }

        public int SalesYear { get; set; }

        public double SalesYearPremium { get; set; }

        public int SalesLastMonth { get; set; }

        public double SalesLastMonthPremium { get; set; }

        public int SalesMonth { get; set; }

        public double SalesMonthPremium { get; set; }

        public int SalesLastWeek { get; set; }

        public double SalesLastWeekPremium { get; set; }

        public int SalesWeek { get; set; }

        public double SalesWeekPremium { get; set; }

        public int SalesYesterday { get; set; }

        public double SalesYesterdayPremium { get; set; }

        public int SalesToday { get; set; }

        public double SalesTodayPremium { get; set; }
    }

The reason why I would like to put each sales period into a list is because I need to iterate through all the possible sales periods in my code and unfortunately I don't have access to change the XML structure.

You should be able to load the XML into an XDocument and extract the information. For example, this would return a Dictionary containing the name of the element (SalesYear, etc.) as the key and its value as the value for every element whose name begins with "Sales":

xDoc.Root.Element("SalesStatisticsPartner").Elements().Where(e => e.Name.LocalName.StartsWith("Sales")).ToDictionary(e => e.Name.Localname, e => e.Value);

If you just want a list of the names of the elements beginning with "Sales" you could do:

xDoc.Root.Element("SalesStatisticsPartner").Elements().Where(e => e.Name.LocalName.StartsWith("Sales")).Select(e => e.Name.LocalName).ToList();

You might have to change the code depending on the exact structure of your XML, but the general approach should work.

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