I always get exception below when try to add attribute, why it's not working?
The prefix '' cannot be redefined from '' to 'http://ws.plimus.com' within the same start element tag.
Code
var docXml = new XElement("param-encryption",
new XAttribute("xmlns", "http://ws.plimus.com"),
new XElement("parameters"));
var s = docXml.ToString();
I want to get result like
<param-encryption xmlns="http://ws.plimus.com">
<parameters>
</parameters>
</param-encryption>
This simplest approach is to let LINQ to XML do this automatically by specifying the namespace in the element name:
XNamespace ns = "http://ws.plimus.com";
var docXml = new XElement(ns + "param-encryption", new XElement(ns + "parameters"));
Result of docXml.ToString()
:
<param-encryption xmlns="http://ws.plimus.com">
<parameters />
</param-encryption>
See more on this question at Stackoverflow