XML 从不同的树中检索 2 个 XElements
XML Retrieving 2 XElements from different trees
我的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 数据库,每个列 period/Labels,以及相应的值。
我已经尝试了以下代码的各种变体,所有这些变体都与值不匹配。
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();
有关如何使它们与数据网格对齐的任何线索,类似于:-
期间值
2007/08 第一季度 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" }
我发现你的 XML 文件有两个问题,第一个 <columns>
标签没有关闭,必须在两个 column
元素之后,第二个也是重要的一个在你的 value
树你又有一个 value
元素(你想要获取),这肯定会产生问题,因为如果你要寻找 value
的后代,那么这个项目也会被考虑会导致意想不到的结果。
因此,我已将您的内部元素替换为列名 amount
,您可以为其指定任何有意义的名称并相应地更改查询:-
var period = xdoc.Descendants("period")
.Select((x,index) => new
{
Period = (string)x.Element("label"),
Value = x.Document.Root.Descendants("value")
.Where((v,i) => i == index)
.Select(z => (string)z.Element("amount")).FirstOrDefault()
});
如您所见,我正在获取 Period & Value 出现在 columns
& [=18 中的相同索引处=]树分别。
我的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 数据库,每个列 period/Labels,以及相应的值。 我已经尝试了以下代码的各种变体,所有这些变体都与值不匹配。
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();
有关如何使它们与数据网格对齐的任何线索,类似于:-
您可以简单地在两个单独的列表中提取句点和值:
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" }
我发现你的 XML 文件有两个问题,第一个 <columns>
标签没有关闭,必须在两个 column
元素之后,第二个也是重要的一个在你的 value
树你又有一个 value
元素(你想要获取),这肯定会产生问题,因为如果你要寻找 value
的后代,那么这个项目也会被考虑会导致意想不到的结果。
因此,我已将您的内部元素替换为列名 amount
,您可以为其指定任何有意义的名称并相应地更改查询:-
var period = xdoc.Descendants("period")
.Select((x,index) => new
{
Period = (string)x.Element("label"),
Value = x.Document.Root.Descendants("value")
.Where((v,i) => i == index)
.Select(z => (string)z.Element("amount")).FirstOrDefault()
});
如您所见,我正在获取 Period & Value 出现在 columns
& [=18 中的相同索引处=]树分别。