Grouping XML Elements using Group By
Posted by codingsense on September 24, 2008
Since you have seen how to create an XML and fetch simple data from it, lets move to next level. In this sample we shall Group the elements and create our own XML from an existing XML.
Lets us consider a clientInfo XML which will store client Name and his Address(City,Area), now consider a situaltion where we would require to list all the employes grouped with their area name, How it can be done?. Here it goes.
XElement GroupAreas = new XElement("Areas", from client in ClientInfo.Elements() group client by client.Element("Address").Element("Area").Value into GroupedNames select new XElement(GroupedNames.Key, GroupedNames.Elements("Name") ));
After this the clients will be grouped on their area name.
Happy Learning 🙂