0

I'm trying to create XDocument based on existing XML file and use it with TreeView. I want to group contacts by the Group element.

XML file structure -

<Contacts>
  <Contact>
    <First_Name>Ren</First_Name>
    <Last_Name>Fuji</Last_Name>
    <Group>Test</Group>
    <Home_Number>Unavailable</Home_Number>
    <Mobile_Number>Unavailable</Mobile_Number>
    <Image></Image>
  </Contact>
<Contacts>

Here is the method which creates XDocument object -

     static XDocument groupXDoc = new XDocument();
     private static void CreateGroupsXML()
            {
                XElement root = new XElement("Groups");
                groupXDoc.Add(root);
                foreach (XElement xelement in xdoc.Element("Contacts").Elements("Contact").ToList())
                {
                    String groupData = (String)xelement.Element("Group").Value;
                    XElement groups = new XElement("Group", new XAttribute("Name", groupData));
                    root.Add(groups);
                    if (groupXDoc.Root.Element("Group").FirstAttribute.Value == xelement.Element("Group").Value)
                    {
                        groups.Add(new XElement("First_Name", (String)xelement.Element("First_Name").Value),
                            new XElement("Last_Name", (String)xelement.Element("Last_Name").Value),
                            new XElement("Home_Number", (String)xelement.Element("Home_Number").Value),
                            new XElement("Mobile_Number", (String)xelement.Element("Mobile_Number").Value)
                            );
                    }
                }
            }

However that's the result I'm getting -

enter image description here

As you can see only the first contact shows correctly, but the other two doesn't. Could someone please help me with this?

1 Answer 1

1
private static XDocument GroupContactsByGroupField(XDocument doc)
    {
        var contacts = doc.Descendants("Contact").Select(x => new
        {
            FirstName = x.Element("First_Name").Value,
            LastName = x.Element("Last_Name").Value,
            Group = x.Element("Group").Value,
            HomeNumber = x.Element("Home_Number").Value,
            MobileNumber = x.Element("Mobile_Number").Value
        });
        var contactsGroupedByGroup = contacts.GroupBy(x => x.Group);

        var newDoc = new XDocument(new XElement("Groups", contactsGroupedByGroup.Select(x =>
           new XElement("Group", new XAttribute("Name", x.Key),
            x.Select(y => new XElement("Contact",
                new XElement("First_Name", y.FirstName),
                new XElement("Last_Name", y.LastName),
                new XElement("Home_Number", y.HomeNumber),
                new XElement("Mobile_Number", y.MobileNumber)
            ))))));
        return newDoc;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.