Remove xmlns attribute with namespace prefix

Neolisk picture Neolisk · Nov 28, 2012 · Viewed 10.2k times · Source

This question is a logical continuation of this one - now suppose an XElement contains elements in a non-default namespace:

<Body xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <ReportItems />        
  <Height />
  <rd:Style />
</Body>

I am trying to follow the same approach as suggested in the answer for my previous question, i.e. remove xmlns attribute, but it does not work when it's xmlns + prefix, like this xmlns:xx.

TL;DR version

This works:

Dim xml = <Body xmlns="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"/>
xml.Attribute("xmlns").Remove()

This doesn't:

Dim xml = <Body xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"/>
xml.Attribute("xmlns:rd").Remove()

Getting this error:

XmlException was unhandled
The ':' character, hexadecimal value 0x3A, cannot be included in a name.

How do I remove xmlns:xx attribute from an XElement?

Answer

fsimonazzi picture fsimonazzi · Nov 28, 2012

Try this instead:

xml.Attribute(XNamespace.Get("http://www.w3.org/2000/xmlns/") + "rd").Remove()