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

标签 c# xml csproj xml-attribute

我从另一个文件将 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/

相关文章:

T4 模板在新的 VS2017 csproj 项目中不生成输出

C# VS 卸载程序

java - 无法使用 JAXB 正确解码包含选项卡的 XML 文件?

sql - Oracle:加载一个大的 xml 文件?

xml - 提取标签名称、属性及其值

c# - CsProj 迁移 : dynamic becoming object on Microsoft. Excel.Interop 程序集

c# - 如何根据已过期的输入文件执行 msbuild (csproj) 目标

c# - 在属性构造函数中使用 Lambda 获取方法的参数

c# - C#中的正则表达式来检测单词之间的多个空格

c# - 为什么在 VB.Net 中每个表单都有一个默认实例,而在 C# 中却没有?