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?
When we want to get information from the XML document stream into an attribute of the FME feature, and that information maps to an attribute with multiple values, then we can either: use a “primitive attribute” and append, with a separator character, the multiple values into one string, or.
attributes cannot contain multiple values (elements can)
The group element is used to define a group of elements to be used in complex type definitions.
You did not explain what result you want but I suspect you want the following grouping:
var documentGroups =
from document in XDocument.Load("input.xml").Descendants("Document")
from productType in document.Element("productTypes").Elements("productType")
group document by (string)productType.Attribute("id");
foreach (var documentGroup in documentGroups)
{
Console.WriteLine("Group {0} has the following members:", documentGroup.Key);
foreach (XElement document in documentGroup)
{
Console.WriteLine("\t{0}", (string)document.Element("title"));
}
Console.WriteLine();
}
With the input being
<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>
</Documents>
that outputs
Group x1 has the following members:
title1
title2
Group x3 has the following members:
title1
title3
Group x2 has the following members:
title4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With