When I run the following code to add an element with an attribute it always adds xlmns=”” at the end. I’ve read that I need to set the namespace for it’s parent or something like that. The thing that’s weird is that in the file the parent item does have the namespace set already so I don’t quite know why that wouldn’t be already be accounted for when the document gets loaded. But either way, I just want to know how to tell it the namespace is there so it will stop adding the xlmns=”” to the end of everything.
XmlDocument xDoc = new XmlDocument();
xDoc.Load(projectFile);
// ...
// ...
var n = xDoc.CreateNode(XmlNodeType.Element, "Compile", null);
var a = xDoc.CreateAttribute("Include");
a.Value = filePath;
n.Attributes.Append(a);
itemGroupNode.AppendChild(n);
xDoc.Save(@"c:\projects\BusinessObjects\BusinessObjects.csproj");
You’re adding an element with namespace
""to an element with namespace"http://schemas.microsoft.com/developer/msbuild/2003". This means that the new element needs anxmlnsattribute.If you add an element with namespace
"http://schemas.microsoft.com/developer/msbuild/2003", noxmlnsattribute is needed (because it’s inherited from the parent element):