简体   繁体   English

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

[英]How to remove namespace from an XML Element using C#

I have an XML where I have a name space _spreadSheetNameSapce . 我有一个XML,我有一个名称空间_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. 它可以正确创建XElement ,但它也会在同一元素中插入xmlns=""条目。 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? 如何在没有空名称空间的情况下创建XElement ,或者如何在创建元素后删除命名空间?

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. 据推测,这是这一个命名空间,这就是为什么它的添加元素中xmlns=""的一部分。 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. 再次强调,这不是关于删除命名空间 - 它是将一个元素放入与其父级继承的“默认”相同的命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM