views:

877

answers:

2

If I have an XElement that has child elements, and if I remove a child element from the parent, removing all references between the two, will the child XElement have the same namespaces as the parent?

In other words, if I have the following XML:

<parent xmlns:foo="abc">
    <foo:child />
</parent>

and I remove the child element, will the child element's xml look like

<child xmlns="abc" />

or like

<child />
+1  A: 

The answer is yes, namespaces do propagate to children.

You do NOT have to specify the namespace within child elements. The scoping of a namespace includes all elements until the closing tag of the element it was defined in.

See section #6.1 here http://www.w3.org/TR/REC-xml-names/#scoping

hope that helps

Xian
+1  A: 

If you include mentioned element in the new xml tree it will be in the same namespace.

var xml1 = XElement.Parse("<a xmlns:foo=\"abc\"><foo:b></foo:b></a>");
var xml2 = XElement.Parse("<a xmlns:boo=\"efg\"></a>");
XNamespace ns = "abc";
var elem = xml1.Element(ns + "b");
elem.Remove();
xml2.Add(elem);
Console.WriteLine(xml1.ToString());
Console.WriteLine(xml2.ToString());

Result:

<a xmlns:foo="abc" />
<a xmlns:boo="efg">
   <b xmlns="abc"></b>
</a>
aku