41

This is my code:

XElement itemsElement = new XElement("Items", string.Empty);
//some code
parentElement.Add(itemsElement);

After that I got this:

<Items xmlns=""></Items>

Parent element hasn't any namespace. What can I do, to get an Items element without the empty namespace attribute?

Christoffer Lette
  • 14,346
  • 7
  • 50
  • 58
GrzesiekO
  • 1,179
  • 4
  • 21
  • 34
  • The parent not having an explicit namespace can mean it inherits it from an ancestor. Does any element in the ancestor chain expose a namespace? – Frédéric Hamidi Aug 20 '12 at 13:33
  • Yes. Some parents have namespace. But direct parent of Items hasn't. What's more. Each element of Items hasn't namespace. – GrzesiekO Aug 20 '12 at 13:36
  • If you're looking for , than you could try remove string.Empty in XElement constructor. – Dmitry Reznik Aug 20 '12 at 13:38
  • I tried it too. But it doesn't working. – GrzesiekO Aug 20 '12 at 13:41
  • 1
    @ogrod, then you should set the same namespace as the one defined in the closest ancestor element, i.e. if you have ``, then you should apply `xmlns="urn:foo"` to the `` element as well. – Frédéric Hamidi Aug 20 '12 at 13:48
  • @Frederic: I did that, and I got Item with the same namespace as parent node. I want to get Item without any namepsace. – GrzesiekO Aug 20 '12 at 13:56
  • 2
    If the element has the same default namespace as the parent, then it should not have an `xmlns` attribute. Are you trying to set that attribute by hand instead of using an `XNamespace`? Can you post the markup of the ancestor element that has a namespace defined? – Frédéric Hamidi Aug 20 '12 at 13:59
  • All right, now it's working :) I copied namespace with attribute from parent element. Now, I add only namespace, without attribute, and it's working. Thank you ;) – GrzesiekO Aug 20 '12 at 14:02
  • you should change your question because the solution is not the answer to what you asked. – JoeBrockhaus Oct 13 '14 at 17:44

1 Answers1

91

It's all about how you handle your namespaces. The code below creates child items with different namespaces:

XNamespace defaultNs = "http://www.tempuri.org/default";
XNamespace otherNs = "http://www.tempuri.org/other";

var root = new XElement(defaultNs + "root");
root.Add(new XAttribute(XNamespace.Xmlns + "otherNs", otherNs));

var parent = new XElement(otherNs + "parent");
root.Add(parent);

var child1 = new XElement(otherNs + "child1");
parent.Add(child1);

var child2 = new XElement(defaultNs + "child2");
parent.Add(child2);

var child3 = new XElement("child3");
parent.Add(child3);

It will produce XML that looks like this:

<root xmlns:otherNs="http://www.tempuri.org/other" xmlns="http://www.tempuri.org/default">
    <otherNs:parent>
        <otherNs:child1 />
        <child2 />
        <child3 xmlns="" />
    </otherNs:parent>
</root>

Look at the difference between child1, child2 and child3. child2 is created using the default namespace, which is probably what you want, while child3 is what you have now.

Christoffer Lette
  • 14,346
  • 7
  • 50
  • 58
  • 1
    I think the code in this answer will result in `"System.Xml.XmlException: 'The ':' character, hexadecimal value 0x3A, cannot be included in a name.'"`. To avoid this use `new XElement(XName.Get("child1", otherNs));` in place of `new XElement(otherNs + "child1");` – cedd Jul 30 '18 at 08:25
  • @cedd Did you try it? It works on my machine. In fact, `Console.Out.WriteLine(root.ToString());` produces exactly what I show in the answer above. I'm running this in a Console app on .Net Framework 4.6.2. What did you do to get this error? – Christoffer Lette Jul 30 '18 at 17:27
  • 1
    @cedd Ok, I can force that exact error if I use `var` when declaring the namespaces. They will then become `string`s, which will not work. They must be explicitly typed as `XNamespace`. – Christoffer Lette Jul 30 '18 at 17:30