0

How would I go about wrapping all the child elements of my root? Example of what I currently have:

<?xml version="1.0" encoding="utf-16"?>
<root>
    <element>YUP</element>
    <element>YUP</element>
    <element>YUP</element>
</root>

But I need it to add a new child that encapsulates all those children of the root like so:

<?xml version="1.0" encoding="utf-16"?>
<root>
    <newsubroot>
         <element>YUP</element>
         <element>YUP</element>
         <element>YUP</element>
    </newsubroot>
</root>

I don't have a preferred (or mandatory) technology - any example using both XmlDocument or XDocument is welcome.

keenthinker
  • 7,645
  • 2
  • 35
  • 45
Yoda
  • 121
  • 1
  • 12
  • Read some article you must, in reading few documentation the solution is! Use your Force. – Matías Fidemraizer Jun 12 '14 at 19:27
  • Hahah can you point me in the right direction then I've tried looking at a few examples and reading MSDN but nothing seems to be quite right. – Yoda Jun 12 '14 at 19:30
  • No, its *your* choice to do it with XDocument or XmlDocument. It sounds like you're asking us to do it for you. Please show what you've tried. – tnw Jun 12 '14 at 19:32
  • 2
    @Yoda In your own skills you don't rely. In the Light side XDocument class is, and LINQ-to-XML together with `XElement` constructor the solution will be. Follow this path: http://stackoverflow.com/questions/4562571/linq-and-xdocument-how-to-create-xml-file – Matías Fidemraizer Jun 12 '14 at 19:33
  • @tnw I was ooking for a suggestion of where to start or what people recommend using xDocument or XmlDocumnt since I know you can use Linq to XML with XDocument. – Yoda Jun 12 '14 at 19:37
  • @MatíasFidemraizer sorry don't use stackoverflow very much. Thanks for the link I'll have a look. – Yoda Jun 12 '14 at 19:38
  • 1
    @pasty Thanks for editing the question and making it more precise. – Yoda Jun 12 '14 at 20:10

3 Answers3

1
XDocument xDoc = XDocument.Parse(
    @"<?xml version=""1.0"" encoding=""utf-16""?>
    <root>
        <element>YUP</element>
        <element>YUP</element>
        <element>YUP</element>
    </root>");

var newsubroot = new XElement("newsubroot");
newsubroot.Add(xDoc.Root.Elements());
xDoc.Root.RemoveAll();
xDoc.Root.Add(newsubroot);
Douglas
  • 53,759
  • 13
  • 140
  • 188
1

One possible solution that uses LINQ2XML could look like this:

  • get the elements
  • make a copy of them (because they are fetched by reference)
  • remove them
  • add new subnode in the root
  • insert the copied elements into the new node

The code looks like this:

    var xml = @"<?xml version=""1.0"" encoding=""utf-16""?>
<root>
    <element>YUP</element>
    <element>YUP</element>
    <element>YUP</element>
</root>";

try
{
    var document = XDocument.Parse(xml);
    var root = document.Root;
    // get all "element" elements
    var yups = root.Descendants("element");
    // get a copy of the nodes that are to be moved inside new node
    var copy = yups.ToList();
    // remove the nodes from the root
    yups.Remove();
    // put them in the new sub node
    root.Add(new XElement("newSubRoot", copy));
    // output or save
    Console.WriteLine(document.ToString()); // document.Save("c:\\xml.xml");
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message);
}

The output is:

<root>
    <newSubRoot>
        <element>YUP</element>
        <element>YUP</element>
        <element>YUP</element>
    </newSubRoot>
</root>
keenthinker
  • 7,645
  • 2
  • 35
  • 45
0

You can use XDocument like below

XDocument doc = XDocument.Parse(xml);
foreach (XElement subroot in doc.Descendants("newsubroot"))
{
    // get subroot elements
    foreach (var subsubroot in subroot.Descendants("element"))
    {
        // get subsubroot elements
    }
}

see this example : Query an XDocument for elements by name at any depth

Community
  • 1
  • 1
mhd
  • 446
  • 5
  • 18