Remove unwanted (empty) xmlns attribute added by appendChild Remove unwanted (empty) xmlns attribute added by appendChild xml xml

Remove unwanted (empty) xmlns attribute added by appendChild


The xmlns="" namespace (un)declaration has been added because your parent element is in a namespace and your child element is not.

If you don't want this namespace declaration added, the implication is that you want the child element to be in the same namespace as its parent, and the answer is to put it in this namespace at the time you create the element. That is, change the call CreateElement("Target", "") to specify the correct namespace.


As answered by Michael Kay, the best way to remove this unwanted namespace is creating the new child element in the same namespace as its parent:

function setupProject($projectFile) {  [xml]$root = Get-Content $projectFile;  $project = $root.Project;  # UPDATE THIS LINE $beforeBuild = $root.CreateElement("Target", "");  $beforeBuild = $root.CreateElement("Target", $project.NamespaceURI);  $beforeBuild.SetAttribute("name", "BeforeBuild");  $beforeBuild.RemoveAttribute("xmlns");  $project.AppendChild($beforeBuild);  $root.Save($projectFile);}


Check these for possible solutions:

Powershell and csproj

Xml namespace and C# csproj

Here is a workaround from the second solution that worked for OP:

$content = [xml] $content.OuterXml.Replace(" xmlns=`"`"", "")$content.Save($_.FullName);