c# - 将 xml 数据规范化为对象 linq to xml

标签 c# xml linq

您好,我有这个 xml,我想获取计划列表和计划条款列表。

   Plan
    {
    name = "Premium"
    List<Terms>{
      24 months/24000 miles
     36 months/36000 miles
    }

    }

XML:

<plan>
    <planid>1</planid>
    <plantitle>Premium</plantitle>
    <plandesc>12 months / 12,000 miles</plandesc>
    <planwarranty>NA</planwarranty>
    <plancoveredterm>12</plancoveredterm>

</plan>
<plan>
    <planid>2</planid>
    <plantitle>Premium</plantitle>
    <plandesc>24 months / 24,000 miles</plandesc>
    <planwarranty>NA</planwarranty>
    <plancoveredterm>24</plancoveredterm>

</plan>
<plan>
    <planid>3</planid>
    <plantitle>Premium</plantitle>
    <plandesc>36 months / 36,000 miles</plandesc>
    <planwarranty>NA</planwarranty>
    <plancoveredterm>36</plancoveredterm>

</plan>

我正在使用以下代码块

XDocument xdoc = XDocument.Parse(providerResponse);
XElement root = xdoc.Root;

var quotePlan = (from planInfo in root.Descendants("plan")
                 select new QuotePlanMbp
                 {
                     Name = planInfo.Element("plantitle").Value.ToString(),
                     QuoteTerms = (from planTerm in root.Descendants("plan")
                                   select new QuoteTermMbp
                                   {
                                       TermMonths = planTerm.Element("plancoveredterm").Value != null ? Convert.ToInt32(planTerm.Element("plancoveredterm").Value) : 0,
                                       TermMiles = planTerm.Element("plancoveredmiles").Value != null ? Convert.ToInt32(planTerm.Element("plancoveredmiles").Value) : 0,
                                       TermCost = planTerm.Element("plancost").Value != null ? Convert.ToDecimal(planTerm.Element("plancost").Value) : 0
                                   }).ToList<QuoteTerm>()
                  }).GroupBy(plan => plan.Name);

这为我提供了三个顶级计划记录,但我只想要一个包含三个术语的计划记录(因为名称始终为“高级”)。 您能否建议一些调整来解决此问题。

最佳答案

在选择术语之前,您应该按名称对计划进行分组。然后为每个计划组选择术语列表:

from planInfo in root.Descendants("plan")
group planInfo by (string)planInfo.Element("plantitle") into g
select new QuotePlanMbp {
   Name = g.Key,
   QuoteTerms = 
      (from planTerm in g
       let months = (int?)planTerm.Element("plancoveredterm")
       select new QuoteTermMbp {
           TermMonths = months.GetValueOrDefault(),
           TermMiles = (int)((int?)planTerm.Element("plancoveredmiles") ?? 0),
           TermCost = ((int?)planTerm.Element("plancost")).GetValueOrDefault()
       }).ToList<QuoteTermMbp>()
};

我还在这里展示了三种不同的技术来解析术语的整数属性 - 选择您更喜欢的一种。

关于c# - 将 xml 数据规范化为对象 linq to xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727548/

相关文章:

java - 为什么 Docx4j 不从 docx 文件中删除内容控件?

c# - 使用 CancelEventArgs 的 Closing() 与使用 FormClosingEventArgs 的 Form_Closing

C# 项目和命名空间问题

c# - 如何使程序不显示在 Alt-Tab 或任务栏中

css - XSLT 测试以检查 <tr> 父节点 (<table>) 是否具有特定 ID

xml - Spring 配置

linq - 是否可以从 Visual Studio 2008 中的 edmx 更新 sql 数据库架构?

asp.net - 用于在自连接表中的嵌套 <ul> 中显示无限类别树的逻辑

c# - 如何将 html 绑定(bind)到 Gridview 列?

C# 对 IEnumerables 的 IEnumerable 进行排序