如何删除XDocument中根以外的节点的xmlns属性? [英] How to remove xmlns attribute of a node other than root in an XDocument?

查看:274
本文介绍了如何删除XDocument中根以外的节点的xmlns属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XDocument尝试删除第一个内部节点上的xmlns=""属性:

I'm using XDocument to try and remove an xmlns="" attribute on the first inner node:

<Root xmlns="http://my.namespace">
    <Firstelement xmlns="">
        <RestOfTheDocument />
    </Firstelement>
</Root>

因此,我想要的是:

<Root xmlns="http://my.namespace">
    <Firstelement>
        <RestOfTheDocument />
    </Firstelement>
</Root>

代码

doc = XDocument.Load(XmlReader.Create(inStream));

XElement inner = doc.XPathSelectElement("/*/*[1]");
if (inner != null)
{
    inner.Attribute("xmlns").Remove();
}

MemoryStream outStream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(outStream);
doc.Save(writer); // <--- Exception occurs here

问题

尝试保存文档时,出现以下异常:

Problem

Upon trying to save the document, I get the following exception:

不能在同一开始内将前缀"从"重新定义为" http://my.namespace "元素标签.

这甚至意味着什么,我该怎么去除那个讨厌的xmlns=""?

What does this even mean and what can I do to remove that pesky xmlns=""?

  • 我确实想保留根节点的名称空间
  • 我只希望删除特定的xmlns,文档中将没有其他xmlns属性.
  • I do want to keep the root node's namespace
  • I only want that specific xmlns removed, there will be no other xmlns attributes in the document.

我尝试使用受这个问题上的答案启发的代码:

I've tried using code inspired from answers on this question:

inner = new XElement(inner.Name.LocalName, inner.Elements());

调试时,xmlns属性已消失,但我遇到了相同的异常.

When debugging, the xmlns attribute is gone from it but I get the same exception.

推荐答案

我认为下面的代码是您想要的.您需要将每个元素放入正确的名称空间,并 删除受影响元素的所有xmlns=''属性.后一部分是必需的,否则LINQ to XML基本上会为您提供

I think the code below is what you want. You need to put each element into the right namespace, and remove any xmlns='' attributes for the affected elements. The latter part is required as otherwise LINQ to XML basically tries to leave you with an element of

<!-- This would be invalid -->
<Firstelement xmlns="" xmlns="http://my.namespace">

这是代码:

using System;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        XDocument doc = XDocument.Load("test.xml");
        // All elements with an empty namespace...
        foreach (var node in doc.Root.Descendants()
                                .Where(n => n.Name.NamespaceName == ""))
        {
             // Remove the xmlns='' attribute. Note the use of
             // Attributes rather than Attribute, in case the
             // attribute doesn't exist (which it might not if we'd
             // created the document "manually" instead of loading
             // it from a file.)
             node.Attributes("xmlns").Remove();
             // Inherit the parent namespace instead
             node.Name = node.Parent.Name.Namespace + node.Name.LocalName;
        }
        Console.WriteLine(doc); // Or doc.Save(...)
    }
}

这篇关于如何删除XDocument中根以外的节点的xmlns属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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