1

I wish to create an xml file with "Package" as the root node, "types" being its child node and further "members" being the child node of "types". Another "name" node which will be a sibiling node of "members" node.

XDocument doc = new XDocument(new XElement("Package"));

            foreach (var group in componentsGroupedByType)
            {
                 doc.Root.Add(new XElement("types"));

                    foreach (var user in group)
                    {
                        doc.Root.Add(new XElement("members", user.Item2));
                    }
                 doc.Root.Add(new XElement("name", group.Key));
            }

Expected Output:

<Package>
   <types>
     <members>xyz</members>
     <members>xyz</members>
     <name>abc</name>
   </types>

   <types>
     <members>xyz</members>
     <members>xyz</members>
     <name>abc</name>
   </types>
</Package>

1 Answer 1

2

The <members> and <name> elements should be added to the <types> element you created, not the document root.

Do this instead:

var doc = new XDocument(
    new XElement("Package",
        from g in componentsGroupedByType
        select new XElement("types",
            from u in g
            select new XElement("members", u.Item2),
            new XElement("name", u.Key)
        )
    )
);
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.