none
Grouping nodes based on an element value RRS feed

  • Question

  • Hi ,

    I have a below xml

    <Detail>

    <Amount>25</Amount>

    <Code>BA</Code>

    </Detail>

    <Detail>

    <Amount>30</Amount>

    <Code>BA</Code>

    </Detail>

    <Detail>

    <Amount>10</Amount>

    <Code>CA</Code>

    </Detail>

    In the above xml, i have to group the Details records having same Codes into one single Detail record and Amount of both should be added into the anount of grouped Detail Record. output should look like below

    <Detail>

    <Amount>55</Amount>

    <Code>BA</Code>

    </Detail>

    <Detail>

    <Amount>10</Amount>

    <Code>CA</Code>

    </Detail>

    Can any one help me out in this?

    Thanks in advance

    Karthik

    Thursday, February 9, 2012 5:25 AM

Answers

  • This can be done very easy with LINQ,  like

                XElement myXML = XElement.Parse(@"<Details>
                                                      <Detail>
                                                        <Amount>25</Amount>
                                                        <Code>BA</Code>
                                                      </Detail>
                                                      <Detail>
                                                        <Amount>30</Amount>
                                                        <Code>BA</Code>
                                                      </Detail>
                                                      <Detail>
                                                        <Amount>10</Amount>
                                                        <Code>CA</Code>
                                                      </Detail>
                                                    </Details>");
                
                
                var myLINQ = from x in myXML.Elements()
                             group x by (string)x.Element("Code") into grp
                             select new 
                                 XElement("Details",
                                 new XElement("Amount", grp.Sum(T => (float)T.Element("Amount"))),
                                 new XElement("Code", grp.Key)
                                 );

    it works as you expected!


    Mãsthän Oli
    Highly Artificial | மெய்ப்பொருள் காண்பதறிவு
    Mark as Answer If you find helpful

    Thursday, February 9, 2012 8:33 AM

All replies

  • Are you doing that via code or do you want XSL script?

    Toni Petrina
    My blog: Programming ramblings
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    Thursday, February 9, 2012 6:24 AM
  • I am doing this via .net code

    karthik

    Thursday, February 9, 2012 7:27 AM
  • You can use LINQ to XML for easy traversal through XML nodes. You can then output new XML.

    Toni Petrina
    My blog: Programming ramblings
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    Thursday, February 9, 2012 8:32 AM
  • This can be done very easy with LINQ,  like

                XElement myXML = XElement.Parse(@"<Details>
                                                      <Detail>
                                                        <Amount>25</Amount>
                                                        <Code>BA</Code>
                                                      </Detail>
                                                      <Detail>
                                                        <Amount>30</Amount>
                                                        <Code>BA</Code>
                                                      </Detail>
                                                      <Detail>
                                                        <Amount>10</Amount>
                                                        <Code>CA</Code>
                                                      </Detail>
                                                    </Details>");
                
                
                var myLINQ = from x in myXML.Elements()
                             group x by (string)x.Element("Code") into grp
                             select new 
                                 XElement("Details",
                                 new XElement("Amount", grp.Sum(T => (float)T.Element("Amount"))),
                                 new XElement("Code", grp.Key)
                                 );

    it works as you expected!


    Mãsthän Oli
    Highly Artificial | மெய்ப்பொருள் காண்பதறிவு
    Mark as Answer If you find helpful

    Thursday, February 9, 2012 8:33 AM