以下是我的xml文件的结构:
<adlibXML>
<recordList>
<record priref="1" selected="False">
<collection>TEXTILES</collection>
<someotherelements>xyz</someotherelements>
</record>
<record priref="2" selected="False">
<collection>CERAMICS</collection>
<someotherelements>xyz</someotherelements>
</record>
<record priref="3" selected="False">
<collection>PAINTING</collection>
<someotherelements>xyz</someotherelements>
</record>
<record priref="4" selected="False">
<collection>TEXTILES</collection>
<someotherelements>xyz</someotherelements>
</record>
</recordList>
我使用来自供应商的数据库中的某些API来获取此xml文件。我的新数据库中有三个表,Catalog,Collection和Artifacts。 我想根据它的值(如'TEXTILES','CERAMICS'等)对collection元素进行分组,并使用一个计数器变量来计算我拥有的集合数。然后使用我可以创建collectionID&amp; collectionTitle在我的Collection表中动态生成。
我无法解决它。直到现在我只完成了这段代码:
var getcontent = from record in xmldoc.Descendants("record")
//this group by doesn't work
group record by record.Element("collection")
select record.Element("collection").Value;
foreach (var node in getcontent)
{
Response.Write("Collection: " + node + "<br/>");
}
答案 0 :(得分:3)
尝试以下几行:
var groups = from record in xmldoc.Descendants("record")
group record by (string)record.Element("collection") into g
select new {
key = g.Key,
count = g.Count()
}
foreach (var group in groups)
{
Response.Write("Collection " + group.key + " has " + group.count + " member(s).");
}