how to remove the xmlns attribute that gets added by the xmldocument

LALITHA KRISHNA 41 Reputation points
2022-05-20T11:45:01.903+00:00

Hi experts ,

i have a scenario, where i have to read the xml document and create/add an xmlnode to the document.

newElem = xmlDocument.CreateNode("element", "newnode", ""); //creates the node
newElem.InnerText ="test" ;
nodeList[0].AppendChild(newElem); //appends the node
string data = xmlDocument.OuterXml; //while assiging the xmldocument to a string variable , its adding attribute xmlns="" to the newnode added

output:
<newnode xmlns="">test<newnode>

how we can remove the attribute xmlns="" that got added by the xml document?

Note : i have newElem.Attributes.RemoveNamedItem("xmlns="); tried this, this was not helpfull

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-05-20T18:59:30.527+00:00

    Try this:

    newElem = xmlDocument.CreateNode( XmlNodeType.Element, "newnode", nodeList[0].NamespaceURI );

    Or show enough details to reproduce the problem.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kamlesh Kumar 3,866 Reputation points
    2022-05-20T12:25:17.46+00:00

    Hi There,

    Thank you for asking this question on the Microsoft Q&A Platform.

    Please check the similar question asked on stackoverflow and this might be helpful for you,

    https://stackoverflow.com/questions/24305747/how-to-remove-xmlns-attribute-of-a-node-other-than-root-in-an-xdocument

    Regards,
    Kamlesh Kumar
    BizTalk Techie

    If this answer solved your problem, please click the Verify Answer button (found below the answer) to help other users who have the same question.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.