removing the root element in xml in java

removing the root element in xml in java

Jon Skeet
people
quotationmark

It sounds like you just want to replace the root element with its single child, right? So you want:

XDocument doc = XDocument.Load(@"C:\Users\ADMIN\Pictures\sample.xml");
doc.Root.ReplaceWith(doc.Root.Elements().Single());

That's all you should need to do. Then doc's root element will be <collection>. I've just tried this, and it was fine.

If you really have to load the document as an XElement (why?) you could always just use:

rootElement = rootElement.Elements().Single();

You don't need to "remove" the root element - just navigate to it.

people

See more on this question at Stackoverflow