从XElement中删除属性 [英] Remove attributes from XElement

查看:0
本文介绍了从XElement中删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑此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>
我想从上面的XML中删除xmlns:xsixmlns:xsd。 我编写了此代码,但它不起作用:

 XAttribute attr = xml.Attribute("xmlns:xsi");
 attr.Remove();

我收到此错误:

其他信息:名称中不能包含十六进制值0x3A的‘:’字符。

如何删除上述属性?

推荐答案

我会使用xml.Attributes().Where(a => a.IsNamespaceDeclaration).Remove()。或使用xml.Attribute(XNamespace.Xmlns + "xsi").Remove()

这篇关于从XElement中删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆