How do I add xml prefixes using XDocument to an existing root element? I have the following XML:
<processSalesOrder>
<header/>
</processSalesOrder>
and I want it to look like:
<ns0:processSalesOrder xmlns:ns0='https://xyx/'>
<header/>
</ns0:processSalesOrder>
In my scenario, I am receiving this xml from web api and passing it long to another web service. The web service is expecting a namespace prefix on the root element.
The web service is from a third party software, it is expecting the xml in a certain format. I tried XmlDocument, XDocument etc ... I couldn't find a way to add the namespace prefix only on the root element. Webservice was rejecting the transactions if the prefix was on Descendants.
Got Reference from here Developer Name: Vijay Sirigiri
If you are trying to add namespace to the elements after loading the xml document then it is not possible.
From MSDN:
However you can load the input, read each element and write it to another document (or in-memory) which has the namespace set. Below is the code that parses the string xml, creates a new xml element along with namespace prefix and namespace.
xmlDocument now contains the xml with each node prefixed with vs.