gpt4 book ai didi

c# - 从文件中添加元素时删除 xmlns =“” 属性

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:54 26 4
gpt4 key购买 nike

我从另一个文件将 XmlElemnt 添加到 csproj 文件:

//load the orginal file
XmlDocument xd = new XmlDocument();
xd.Load(fileName);

//load the csproj file to setting
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(project.FullName);

//copy a XmlNode from the orginal file
XmlNode copiedNode = xmlDoc.ImportNode(xd.SelectSingleNode(nodeName), true);
//add the XmlNode to the csproj file
xmlDoc.DocumentElement.InsertAfter(copiedNode,xmlDoc.GetElementsByTagName(nodeName).Item(0));

并且源代码自动将属性 xmlns=""添加到添加的节点:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'AAA|x86'" xmlns=""> 

我在帖子中看到了类似的问题: Remove xmlns=“” attribute when adding Reference element into csproj .解决方案是添加命名空间,但我找不到如何将命名空间添加到我的代码中。

我该怎么做?或者-是否有其他方法可以避免添加 xmlns 属性?

最佳答案

有两种可能的解决方案:

第一个是设置原文件根节点的命名空间:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'AAA|x86'" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

在这种情况下,原始元素的命名空间将与目标根命名空间相同,并且不会添加 xmlns 属性。

如果不可能,您需要更改程序中的 namespace 。但不允许修改已加载的 XNodes。 It can help.

关于c# - 从文件中添加元素时删除 xmlns =“” 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com