Because you're not adding your namespace declaration, the dyn
namespace becomes the default.
Then, when you add the child element without a namespace, a namespace declaration with no namespace has to be added to indicate it is not within the default namespace.
If your dyn
namespace is not meant to be the default namespace, try the following code:
XNamespace dyn = "https://www.abc.at/dyn";
XElement positions = new XElement(
dyn + "Positions",
new XAttribute(XNamespace.Xmlns + "dyn", "https://www.abc.at/dyn"),
new XElement("Vector2-Array"));
This produces the following output:
<dyn:Positions xmlns:dyn="https://www.abc.at/dyn">
<Vector2-Array />
</dyn:Positions>
Note that when you start appending this element to other documents, you may get more behaviour similar to your original problem if there's any mismatches of namespaces.
The OP has specifically brought up the subject of appending this element to another element that also contains the namespace declaration.
I've created this code to test:
XNamespace dyn = "https://www.abc.at/dyn";
XElement positions = new XElement(
dyn + "Positions",
new XAttribute(XNamespace.Xmlns + "dyn", "https://www.abc.at/dyn"),
new XElement("Vector2-Array"));
XElement root = new XElement(
dyn + "root",
new XAttribute(XNamespace.Xmlns + "dyn", "https://www.abc.at/dyn"));
root.Add(positions);
When using the debugger, the XML of the root
element after adding Positions
is this:
<dyn:root xmlns:dyn="https://www.abc.at/dyn">
<dyn:Positions xmlns:dyn="https://www.abc.at/dyn">
<Vector2-Array />
</dyn:Positions>
</dyn:root>
So the namespace declaration is duplicated.
However, there is a SaveOption
of OmitDuplicateNamespaces
that can be used when saving or formatting the XML to string:
Console.WriteLine(root.ToString(SaveOptions.OmitDuplicateNamespaces));
The resulting output of this is as below:
<dyn:root xmlns:dyn="https://www.abc.at/dyn">
<dyn:Positions>
<Vector2-Array />
</dyn:Positions>
</dyn:root>
Because the duplicate namespace declarations effectively do nothing (even though they're ugly) they can be removed this way, if the displaying of the XML is the important thing.
Functionally, having duplicated namespace declarations doesn't actually do anything as long as they match.