3

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Nair
  • 7,438
  • 10
  • 41
  • 69
  • possible duplicate of [How to remove all namespaces from XML with C#?](http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c) – H H Jun 08 '10 at 17:53
  • 1
    Found the problem, the code should be new XElement(_SpreadSheetNameSpace + "Row" .... – Nair Jun 08 '10 at 18:14

1 Answers1

9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194