Here is an example that outputs xml without empty namespaces. Notice the bizarre Linq-centric syntax rootNamespace + "MyElementName", which is the secret. This is the same namespace as the whole document, thus no xmlns addition is necessary. This is concatenating an XNamespace + a string, which is a "+" overload that works for Linq and that Linq knows how to deal with. (Without Linq it could be a compile error to concatenate a string and a non string type). Note this was executed against a C# project file which is a handy Xml file. Output it to a console or a richtextbox control. Then take out the "rootNamespace +" and notice the difference.
XDocument doc = null;
using (StreamReader streamReader =
new StreamReader(@"myXml.csproj"))
{
doc = XDocument.Load(streamReader, LoadOptions.None);
}
XNamespace rootNamespace = doc.Root.Name.NamespaceName;
// A search which finds the ItemGroup which has Reference
// elements and returns the ItemGroup XElement.
XElement element = doc.Descendants().Where(p => p.Name.LocalName == "ItemGroup"
&& p.Descendants().First<XElement>().Name.LocalName == "Reference").First<XElement>();
// Create a completly new element with sub elements.
XElement referenceElement = new XElement(rootNamespace + "Reference",
new XElement(rootNamespace + "SpecificVersion", bool.FalseString),
new XElement(rootNamespace + "HintPath", "THIS IS A HINT PATH"));
// Add the new element to the main doc, to the end of the Reference elements.
element.Add(referenceElement);
// Add an attribute after the fact for effect.
referenceElement.SetAttributeValue("Include", "THIS IS AN INCLUDE");
rtb.Text = doc.ToString(SaveOptions.None);