简体   繁体   中英

Remove xmlns=“” attribute when adding element from file

I add XmlElemnt to csproj file from another file:

//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));

and the source code add automatically the attribute xmlns="" to the added node:

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

I see a similar issue in the post: Remove xmlns=“” attribute when adding Reference element into csproj . The solution there is to add namespace, but I don't find how to add namespace to my code.

How can I do that? Or- Is there other way to avoid the adding of the xmlns attribute?

There are two possible solutions for you:

The first one is to set namespace of the root node of original file:

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

In this case the original element's namespace will be the same as destination root namespace and the xmlns attribute will not be added.

If it is not possible, you need to change namespace in your program. But it is not allowed to modify loaded XNodes by design. It can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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