vb.net 删除带有命名空间前缀的 xmlns 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13615019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:22:09  来源:igfitidea点击:

Remove xmlns attribute with namespace prefix

c#xmlvb.netxelement

提问by Neolisk

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

这个问题的一个合乎逻辑的延续这一个-现在假设一个XElement包含非默认命名空间的元素:

<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 xmlnsattribute, but it does not work when it's xmlns + prefix, like this xmlns:xx.

我正在尝试采用与我上一个问题的答案中建议的相同的方法,即删除xmlns属性,但是当它是 xmlns + 前缀时它不起作用,就像这样xmlns:xx

TL;DR version

TL;DR 版本

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:xxattribute from an XElement?

如何xmlns:xx从 中删除属性XElement

采纳答案by fsimonazzi

Try this instead:

试试这个:

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