Suppress blank namespace in XML element



I have an XML element I am creating. I want Xmlns information to appear in the parent element but no references in any child elements. To acheive this, I am using the following code:



XNamespace xsi = "http://ift.tt/ra1lAU";
XNamespace xsd = "http://ift.tt/tphNwY";
XNamespace xns = "http://ift.tt/1CsVxqW";
var xroot = new XElement(xns + "Constraints", new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName), new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName));
var keyElement = new XElement("Keys");
var kElement = new XElement("K");
kElement.Value = "HDD";
keyElement.Add(kElement);
xroot.Add(keyElement);
xroot.Add(new XElement("Properties"));
xroot.Add(new XElement("MustIncludeKeys"));


What I am getting is:



<Constraints xmlns:xsd="http://ift.tt/tphNwY" xmlns:xsi="http://ift.tt/ra1lAU" xmlns="http://ift.tt/1CsVxqW">
<Keys xmlns="">
<K>HDD</K>
</Keys>
<Properties xmlns="" />
<MustIncludeKeys xmlns="" />
</Constraints>


What I want is:



<Constraints xmlns:xsd="http://ift.tt/tphNwY" xmlns:xsi="http://ift.tt/ra1lAU" xmlns="http://ift.tt/1CsVxqW">
<Keys>
<K>HDD</K>
</Keys>
<Properties />
<MustIncludeKeys />
</Constraints>


What can I do to get rid of the extraneous (blank) name spaces "xmlns="" "? I've tried keyElement.ReplaceAttributes("") and even tried using the System.Xml namespace to use the RemoveAttribute Method - to no avail.


No comments:

Post a Comment