Remove attributes from XElement

c#, c#-4.0, linq-to-xml, xml

Solution

I would use `xml.Attributes().Where(a => a.IsNamespaceDeclaration).Remove()`. Or use `xml.Attribute(XNamespace.Xmlns + "xsi").Remove()`.

Problem

Please consider this XElement: ``` <MySerializeClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <F1>1</F1> <F2>2</F2> <F3>nima</F3> </MySerializeClass> ``` I want to delete `xmlns:xsi` and `xmlns:xsd` from above XML. I wrote this code but it does not work: ``` XAttribute attr = xml.Attribute("xmlns:xsi"); attr.Remove(); ``` I got this error: Additional information: The ':' character, hexadecimal value 0x3A, cannot be included in a name. How I can delete above attributes?

Original source

Related problems