I want to detach some XmlElement from one XmlDocument in order to insert it into another. But when I do this, descendant nodes with namespaces automatically get xmlns attribute appended. By some reason the target xml file fails to pass custom validation with all children namespaces set. Is there an option to supress xmlns propagation to descendants?
The following code illustrates the issue – the execution result is <MakesMeSick:three xmlns:MakesMeSick="http://tempuri.org" />
using System.Xml;
class Program
{
static string xml = @"
<mydoc>
<one xmlns:MakesMeSick=""http://tempuri.org"">
<two>
<MakesMeSick:three />
</two>
</one>
</mydoc>
";
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode n = doc.SelectSingleNode("/mydoc/one/two");
System.Console.WriteLine(n.InnerXml);
}
}
If you have an element of the form
<prefix:localname>then you must have anxmlns:prefix="whatever"somewhere in that element or an ancestor that explains what URIprefixmaps to. It just doesn’t make sense to have a prefix with no associated URI.So, in your example, the output
<MakesMeSick:three xmlns:MakesMeSick="http://tempuri.org" />is correct. If thexmlnswas missing then it would not be a valid XML document.