c# - Get rid of xmlns attribute when adding node to existing xml -
i have xml-file:
<ns2:root xmlns:ns2="namespace"> <ns2:a> <ns2:b>some content</b> <ns2:c>some content</c> <ns2:d>some content</d> </a> </root>
i need add new node in specific place, code is:
var doc = xdocument.load(file); xnamespace ns2 = "namespace"; doc.element(ns2 + "root").element(ns2 + "a").element(ns2 + "c").addafterself( new xelement(ns2+"new", new xelement("new1", new xelement("new2","some content"), new xelement("new3", "some content"))));
the output is:
<ns2:root xmlns:ns2="namespace"> <ns2:a> <ns2:b>some content</b> <ns2:c>some content</c> <ns2:new> <new1 xmlns=""> <new2>some content</new2> <new3>some content</new3> </new1> </new> <ns2:d>some content</d> </a> </root>
desired output is:
<ns2:root xmlns:ns2="namespace"> <ns2:a> <ns2:b>some content</b> <ns2:c>some content</c> <ns2:new> <new1> <new2>some content</new2> <new3>some content</new3> </new1> </new> <ns2:d>some content</d> </a> </root>
how can avoid adding xmlns atrribute node new1?
edited mistake in desired output.
add "ns2 +" before every element name needs in sitemap namespace
var doc = xdocument.load(file); xnamespace ns2 = "namespace"; doc.element(ns2 + "root").element(ns2 + "a").element(ns2 + "c").addafterself( new xelement(ns2+"new", new xelement(ns2+"new1", new xelement(ns2+"new2","some content"), new xelement(ns2+"new3", "some content"))));
Comments
Post a Comment