如何使用 C# 从 XML 元素中删除命名空间

发布于 2024-09-04 08:25:14 字数 394 浏览 1 评论 0原文

我有一个 XML,其中有一个名称空间 _spreadSheetNameSapce。在我的代码中,我必须添加一个新元素,其属性与空间名称相关联,我的操作如下:

XElement customHeading = new XElement("Row",
    new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0"));

它正确创建了 XElement 但它确实插入了 xmlns=""< /code> 条目也在同一元素中。我不想创建该元素。如何在没有空命名空间的情况下创建 XElement ,或者如何在创建元素后删除命名空间?

I have an XML where I have a name space _spreadSheetNameSapce. In my code I have to add a new element with attribute associated with the name the space and I am doing it like the following

XElement customHeading = new XElement("Row",
    new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0"));

It creates the XElement properly but it does insert xmlns="" entry also in the same element. I do not want that element to be created. How can I create the XElement without the empty name space or how can I remove the namespace after the element is created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-09-11 08:25:14

您的代码当前正在创建一个没有命名空间的元素。据推测,这是在命名空间中的元素内,这就是它添加 xmlns="" 部分的原因。如果您只想将其保留在同一个命名空间中,只需使用:

XElement customHeading = new XElement(_spreadSheetNameSapce + "Row",
        new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0"));

再次强调,这并不是要删除命名空间 - 而是将元素放入与“默认”相同的命名空间中从其父母继承。

Your code is currently creating an element without a namespace. Presumably this is within an element which is in a namespace, which is why it's adding the xmlns="" part. If you just want it to keep within the same namespace, just use:

XElement customHeading = new XElement(_spreadSheetNameSapce + "Row",
        new XAttribute(_spreadSheetNameSapce + "AutoFitHeight", "0"));

Just to stress again, this isn't about removing a namespace - it's about putting an element into the same namespace as the "default" inherited from its parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文