This document already has a ‘ DocumentElement ‘ node

I faced this exception while trying to generate a xml document programmatically in C# using XmlDocument and XmlElement.

I didn’t know what was going on, and then I figured out that I was adding multiple elements to the XmlDocument. To solve this, add a “root” element to XmlDocument and then add multiple child elements within the root. The XmlDocument is supposed to only contain 1 child element as expected in XML.

Leave a Reply

Your email address will not be published. Required fields are marked *