Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group XML data by multiple value

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 &amp; 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 &amp; Sustainability</productType>
    </productTypes>
</Document>
<Document>
    <id>4</id>
    <title>title4</title>
    <productTypes>
        <productType id="x2">Defense, Risk &amp; 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?

like image 273
drazen Avatar asked May 26 '12 12:05

drazen


People also ask

How do I pass multiple values in XML?

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.

Can XML attribute have multiple values?

attributes cannot contain multiple values (elements can)

What is group element in XML?

The group element is used to define a group of elements to be used in complex type definitions.


1 Answers

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 &amp; 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 &amp; Sustainability</productType>
    </productTypes>
  </Document>
  <Document>
    <id>4</id>
    <title>title4</title>
    <productTypes>
      <productType id="x2">Defense, Risk &amp; 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
like image 195
Martin Honnen Avatar answered Sep 29 '22 16:09

Martin Honnen