Answered How to solve Linq to XML query

  • Tuesday, February 05, 2013 4:52 PM
     
      Has Code

    Hi,

    I have this structure,

    <Line>
        <ItemList>
            <ServiceNo>441234323115</ServiceNo>
            <Item SortKey="AAAA">
                <Offer>Offer A<PORef></PORef>
                    <Amount>24.54</Amount>
                    <Amount>31.70</Amount>
                    <Date>2012-10-08</Date>
                </Offer>
                <Offer>Offer Z<PORef></PORef>
                    <Amount>4.54</Amount>
                    <Amount>31.70</Amount>
                    <Date>2012-10-08</Date>
                </Offer>
            </Item>
            <Item SortKey="YYYY">
                <Offer>OfferB<PORef></PORef>
                    <Amount>24.54</Amount>
                    <Amount>31.70</Amount>
                    <Date>2012-10-08</Date>
                </Offer>
            </Item>
        </ItemList>
        <ItemList>
            <ServiceNo>447797242822</ServiceNo>
            <Item SortKey="AAAA">
                <Offer>Offer C<PORef></PORef>
                    <Amount>27.10</Amount>
                    <Amount>35.00</Amount>
                    <Date>2012-10-08</Date>
                </Offer>
            </Item>
            <Item SortKey="YYYY">
                <Offer>Offer D<PORef></PORef>
                    <Amount>27.10</Amount>
                    <Amount>35.00</Amount>
                    <Date>2012-10-08</Date>
                </Offer>
            </Item>
        </ItemList>
    </Line>

    I would like to write a Linq query that would give me back all <Offer> elements grouped by Item/@SortKey - This I can do however what I need to do is in each <Offer>, add an Attribute of the parent <ServiceNo> i.e. I would end up with the first group with would be all the <Offer> under SortKey="AAA" BUT the first offer would have the ServiceNo as an attribute of each offer like below

    <Group SortKey="AAA">
        <Offer ServiceNo="441234323115">Offer A<PORef></PORef>
            <Amount>24.54</Amount>
            <Amount>31.70</Amount>
            <Date>2012-10-08</Date>
        </Offer>
        <Offer ServiceNo="441234323115">Offer Z<PORef></PORef>
            <Amount>4.54</Amount>
            <Amount>31.70</Amount>
            <Date>2012-10-08</Date>
        </Offer>
        <Offer ServiceNo="447797242822">Offer C<PORef></PORef>
            <Amount>27.10</Amount>
            <Amount>35.00</Amount>
            <Date>2012-10-08</Date>
        </Offer>
    </Group>



    Any help gratefully received

    Mike



    • Edited by AgileChap Tuesday, February 05, 2013 5:03 PM
    •  

All Replies

  • Tuesday, February 05, 2013 5:04 PM
     
     Answered

    Try along these lines:

    XDocument input = XDocument.Load("input.xml");

    XDocument output = new XDocument(

      new XElement("Groups",

        from offer in input.Descendants("Offer")

        group offer by (string)offer.Parent.Attribute("SortKey") into g

        select new XElement("Group",

         new XAttribute("SortKey", g.Key),

         from gMember in g

         select new XElement("Offer",

           new XAttribute("ServiceNo", (string)gMember.Parent.Parent.Element("ServiceNumber")),

           gMember.Nodes()

        )

      ));


    MVP Data Platform Development My blog

    • Marked As Answer by AgileChap Tuesday, February 05, 2013 6:22 PM
    •  
  • Tuesday, February 05, 2013 6:21 PM
     
     

    Martin,

    Thanks so much for the reply. That's absolutely perfect - Thanks you!

    Mike