Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove attributes from XElement

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?

like image 908
Arian Avatar asked Jul 07 '14 10:07

Arian


1 Answers

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

like image 91
Martin Honnen Avatar answered Sep 29 '22 17:09

Martin Honnen