C#-如何从XElement删除xmlns [英] C# - How to remove xmlns from XElement

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

问题描述

如何从XElement中删除"xmlns"命名空间?

How can I remove the "xmlns" namespace from a XElement?

我尝试过:attributes.remove,xElement.Name.NameSpace.Remove(0)等,等等.

I tried: attributes.remove, xElement.Name.NameSpace.Remove(0), etc, etc. No success.

我的xml:

<event xmlns="http://www.blablabla.com/bla" version="1.00">
  <retEvent version="1.00">
  </retEvent>
</event>

我该怎么做?

推荐答案

接受的答案对我不起作用,因为xelement.Attributes()为空,它没有将名称空间作为属性返回.

The accepted answer did not work for me because xelement.Attributes() was empty, it wasn't returning the namespace as an attribute.

以下内容将删除您的情况下的声明:

The following will remove the declaration in your case:

element.Name = element.Name.LocalName;

如果要对元素进行递归操作,并且所有子元素都使用以下内容:

If you want to do it recursively for your element and all child elements use the following:

    private static void RemoveAllNamespaces(XElement element)
    {
        element.Name = element.Name.LocalName;

        foreach (var node in element.DescendantNodes())
        {
            var xElement = node as XElement;
            if (xElement != null)
            {
                RemoveAllNamespaces(xElement);
            }
        }
    } 

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

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