XML marshal a struct without an xmlns

I’d like to marshal a struct without getting an xmlns added; as far as I can tell one will always gets added with an “” if none exists, once a tag further up has been added.

i.e. I want ‘bar2’ to come out as just plain ‘’, not as ‘’

I’ve tracked it down to here, but I don’t see anyway of suppressing the addition of the empty xmlns looking at that conditional.

Is there anyway of doing this, or do I just need to add an explicit xmlns at every level?

Thanks,
Jon

The comment in the marshaler code doesn’t say this explicitly but this behavior is reasonable. Think what not having an xmlns means from an XML reader’s point of view: “This element inherits the namespace from its parent.” But you initialize the inserted element with an empty namespace, which is different from the parent. So now the marshaler says “Attention, we are changing namespaces here.”

I recommend taking a look this discussion on empty namespace.

Thanks for the reply; yes, its not that the marshaler is wrong here - the caller may indeed want xmlns=“”. But as you said, there’s two cases, for no namespace+inherit, and for the the global namespace, and the marshaller code has no way to distinguish between them. I guess this is just how the types have been setup - if ‘Space’ was a pointer to string then we could have used ‘nil’ for no namespace and the empty string for the global namespace.

Anyway, if I understand it correctly, there’s no way to get the behaviour of no namespace at all with the generic marshaller, so I think I’ll need to go down the path of adding a marshal method to my struct, then I’m free to construct it as I wish.