Nair
Nair

Reputation: 7458

How to remove namespace from an XML Element using C#

I have an XML where I have a name space _spreadSheetNameSapce. In my code I have to add a new element with attribute associated with the name the space and I am doing it like the following

XElement customHeading = new XElement("Row",
    new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0"));

It creates the XElement properly but it does insert xmlns="" entry also in the same element. I do not want that element to be created. How can I create the XElement without the empty name space or how can I remove the namespace after the element is created?

Upvotes: 3

Views: 4687

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

Your code is currently creating an element without a namespace. Presumably this is within an element which is in a namespace, which is why it's adding the xmlns="" part. If you just want it to keep within the same namespace, just use:

XElement customHeading = new XElement(_spreadSheetNameSapce + "Row",
        new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0"));

Just to stress again, this isn't about removing a namespace - it's about putting an element into the same namespace as the "default" inherited from its parent.

Upvotes: 9

Related Questions