I need to group “Document” value from XML. Problem is because key value (productType) can be multiple.
This is XML:
<Documents>
<Document>
<id>1</id>
<title>title1</title>
<productTypes>
<productType id="x1">Capital Costs Analysis Forum - Brazil</productType>
<productType id="x3">Environmental, Health and Safety & Sustainability</productType>
</productTypes>
</Document>
<Document>
<id>2</id>
<title>title2</title>
<productTypes>
<productType id="x1">Capital Costs Analysis Forum - Brazil</productType>
</productTypes>
</Document>
<Document>
<id>3</id>
<title>title3</title>
<productTypes>
<productType id="x3">Environmental, Health and Safety & Sustainability</productType>
</productTypes>
</Document>
<Document>
<id>4</id>
<title>title4</title>
<productTypes>
<productType id="x2">Defense, Risk & Security</productType>
</productTypes>
</Document>
And this is what I try:
var documents = from document in some.Descendants("Document")
group document by (string)document
.Element("productTypes")
.Elements("productType")
.First() into docGroup
select docGroup;
My code is working only if is there one productType element. How to change my code to work if there is multiple value of productType?
You did not explain what result you want but I suspect you want the following grouping:
With the input being
that outputs