[英]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. 我有一个问题,我试图将所有销售期间(SalesYear,SalesToday ...)XML元素放入自定义C#列表中,其中所有销售期间都应为项目。
This is the returned XML I'm deserializing: 这是我要反序列化的返回XML:
<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: 这是我当前反序列化的C#类:
[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. 之所以要将每个销售期都放入一个列表中,是因为我需要遍历代码中所有可能的销售期,但是不幸的是,我无权更改XML结构。
You should be able to load the XML into an XDocument
and extract the information. 您应该能够将XML加载到
XDocument
并提取信息。 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": 例如,这将返回一个Dictionary,其中包含元素名称(SalesYear等)作为键,并且其值作为每个名称以“ 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. 您可能必须根据XML的确切结构来更改代码,但是一般方法应该可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.