3

Following is my requirement. I am reading an xml file (*.csproj file) and searching for a node in it. After I find the node I will insert my element into it. Following is my original XML:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
    <ClInclude Include="Stdafx.h" />
    <ClInclude Include="NewFile.h" />
  </ItemGroup>
</Project>

Following is my code snippet to do this.

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(inputFile);

        XmlNamespaceManager nsMgr = new XmlNamespaceManager(xDoc.NameTable);
        string strNamespace = xDoc.DocumentElement.NamespaceURI;
        nsMgr.AddNamespace("ns", strNamespace);
        XmlNode root = xDoc.SelectSingleNode("/ns:Project/ns:ItemGroup/ns:ClInclude", nsMgr);

        XmlAttribute attr = xDoc.CreateAttribute("Include");
        attr.Value = "NewHeaderFile.h";

        XmlElement xele = xDoc.CreateElement("ClInclude");
        xele.Attributes.Append(attr);

        root.ParentNode.AppendChild(xele);
        xDoc.Save(outFile);

This is the output what i get.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ClInclude Include="Stdafx.h" />
    <ClInclude Include="NewFile.h" />
    <ClInclude Include="NewHeaderFile.h" xmlns="" />
  </ItemGroup>
</Project>

Problem Statement: I want to ignore the xmlns="" in my output. My output should look like this.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ClInclude Include="Stdafx.h" />
    <ClInclude Include="NewFile.h" />
    <ClInclude Include="NewHeaderFile.h" />
  </ItemGroup>
</Project>

Kinldy help me. Thanks for your valuable time.

1
  • Take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it will be for users to help you. Commented Dec 11, 2014 at 17:57

2 Answers 2

4

Change your element declaration with following and should work

XmlElement xele = xDoc.CreateElement("ClInclude", xDoc.DocumentElement.NamespaceURI);

Add the namespace on root mean that your element are in the 'root' namespace, so there is no need to add a 'no namespace' to new element

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help @InvernoMuto. After changing the element declaration it worked as I expected.
3

All elements in the original document are in namespace xmlns="http://schemas.microsoft.com/developer/msbuild/2003", whereas you are creating the new ClInclude element in the empty namespace "". If you also create this element in xmlns="http://schemas.microsoft.com/developer/msbuild/2003", the extraneous xmlns="" will be omitted from the output xml.

1 Comment

Thanks for the help @StuartLC. It worked as you have stated. I created the element with the original namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.