gpt4 book ai didi

c# - XML 从不同的树中检索 2 个 XElements

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:20 25 4
gpt4 key购买 nike

我的 XML 文件示例如下:-

<table>  
<columns>
<column>
<area>
<identifier>E31000040</identifier>
<label>Gtr Manchester Fire</label>
<altLabel>Gtr Manchester Fire</altLabel>
<isSummary>false</isSummary>
</area>
<metricType>
<identifier>948</identifier>
<label>Accidental dwelling fires</label>
<altLabel>Accidental dwelling fires</altLabel>
<isSummary>false</isSummary>
</metricType>
<period>
<identifier>fq_Q1_2007_08</identifier>
<label>2007/08 Q1</label>
<altLabel>2007/08 Q1</altLabel>
<isSummary>false</isSummary>
</period>
<valueType>
<identifier>raw</identifier>
<label>Raw value</label>
<isSummary>false</isSummary>
</valueType>
</column>
<column>
<area>
<identifier>E31000040</identifier>
<label>Gtr Manchester Fire</label>
<altLabel>Gtr Manchester Fire</altLabel>
<isSummary>false</isSummary>
</area>
<metricType>
<identifier>948</identifier>
<label>Accidental dwelling fires</label>
<altLabel>Accidental dwelling fires</altLabel>
<isSummary>false</isSummary>
</metricType>
<period>
<identifier>fq_Q2_2007_08</identifier>
<label>2007/08 Q2</label>
<altLabel>2007/08 Q2</altLabel>
<isSummary>false</isSummary>
</period>
<valueType>
<identifier>raw</identifier>
<label>Raw value</label>
<isSummary>false</isSummary>
</valueType>
</column>
<rows>
<row>
<values>
<value>
<source>732.0</source>
<value>732.0</value>
<formatted>732</formatted>
<format>#,##0</format>
<publicationStatus>Published</publicationStatus>
</value>
<value>
<source>659.0</source>
<value>659.0</value>
<formatted>659</formatted>
<format>#,##0</format>
<publicationStatus>Published</publicationStatus>
</value>
</values>
</row>
</rows>
</table>

首先,我想添加到一个网格,并最终保存到一个 sql 数据库,每个列周期/标签,以及相应的值。我已经尝试了以下代码的各种变体,所有这些变体都不匹配句点和值。

XDocument xd = XDocument.Load(xml);

List<Results> period = new List<Results>();

var columnnodes = xd.Element("table")
.Elements("columns")
.Elements("column")
.Elements("period");

var rownodes = xd.Element("table")
.Elements("rows")
.Elements("row")
.Elements("values")
.Elements("value");

foreach (XElement ele in columnnodes)
{
Results newResult = new Results();
newResult.period = (string)ele.Element("label");

/*newResult.value = (int)xd.Element("table")
.Elements("rows")
.Elements("row")
.Elements("values")
.Elements("value")
.Elements("Formatted");

period.Add(newResult);*/

foreach (XElement ele2 in rownodes)
{
newResult.value = (int)ele2.Element("formatted");
}

period.Add(newResult);
}

gridResults.DataSource = period;
gridResults.DataBind();

关于如何让它们与数据网格对齐的任何线索类似于:-

  • 期间值(value)
  • 2007/08 Q1 732
  • 2007/08 Q2 659
  • 最佳答案

    您可以简单地在两个单独的列表中提取句点和值:

    var periods = from p in xmlDoc.Descendants("period")
    select p.Element("label").Value;

    var values = from v in xmlDoc.Descendants("formatted")
    select v.Value;

    然后将它们.Zip在一起:

    var results = periods.Zip(values, (p, v) => new { Period = p, Value = v });

    生成所需的结果集:

    [0] = { Period = "2007/08 Q1", Value = "732" }
    [1] = { Period = "2007/08 Q2", Value = "659" }

    关于c# - XML 从不同的树中检索 2 个 XElements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27985604/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com