Remove xmlns attribute with namespace prefix

10,286

Try this instead:

xml.Attribute(XNamespace.Get("http://www.w3.org/2000/xmlns/") + "rd").Remove()
Share:
10,286
Victor Zakharov
Author by

Victor Zakharov

MCSD: Web Applications (since 12/14/2013). MCPD: Windows Developer .NET 3.5 (since 09/16/2012). Specializing in Angular / Typescript / .NET Core, in this order, based on recency. My Pluralsight profile: https://app.pluralsight.com/profile/victor-zakharov

Updated on June 22, 2022

Comments

  • Victor Zakharov
    Victor Zakharov almost 2 years

    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?