2

I need to produce an xml document where all elements and attributes are prefixed with (the same) namespace (I know this is not ideal, but unfortunately is needed for interoperation with InfoPath). Using the .NET XmlSerializer, initialized with the right namepsace and prefix, I generally have no problem generating prefixed xml:

xmlSerializer = new XmlSerializer(typeof(T));
xmlNamespaces = new XmlSerializerNamespaces();
xmlNamespaces.Add("foo", "www.namespace.com");

...

[XmlRoot(Namespace = "www.namespace.com")]
public class label
{
    [XmlAttribute(Namespace = "www.namespace.com")]
    public string id { get; set; }

    [XmlElement(Namespace = "www.namespace.com")]
    public string text { get; set; }  
}  

This generates xml

<foo:label id="0" xmlns:foo="www.namespace.com">
    <foo:text>content</foo:text>
</foo:label>

The problem is this: the prefix is applied to everything, except the "id" attribute in the same namespace.

I thought this might be behavior prescribed by W3C, and that attributes declared as belonging to a prefixed element would inherit that prefix. However, it seems that attributes that do not have an associated namespace/prefix do not behave like this - see XML namespaces and attributes and here, which states:

"An attribute never inherits the namespace of its parent element. For that reason an attribute is only in a namespace if it has a proper namespace prefix"

In this case, shouldn't the serializer generate a prefix to show the attribute is in that namespace? Or is this not correct?

Thanks in advance!

MORE INFO: Further investigation (see SamuelNeff's answer below) has determined that unprefixed attributes do not inherit the namespace of their containing element. Does this mean the XmlSerializer is producing off-spec attributes? Is there a way to force it to add the prefix? It will add a prefix if a different namespace uri is added in the XmlAttribute attribute.

Community
  • 1
  • 1
Tryptamine42
  • 79
  • 2
  • 8

2 Answers2

1

OOPS: My interpretation of spec is wrong. This incorrect response is marked as the answer, so I can't delete it. Sorry.

Attributes without prefixes are in the same namespace as their containing element. You only need to apply a prefix to an attribute if the attribute has a namespace different from its element.

Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

http://www.w3.org/TR/2009/REC-xml-names-20091208/#defaulting

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • Ah, that's great, thanks @SamuelNeff. I thought it would be surprising for the serializer to break namespaces so flagrantly. So this - http://stackoverflow.com/a/43226/1603434 - presumably, is just incorrect? – Tryptamine42 Jul 30 '13 at 16:48
  • 1
    @Tryptamine42, looks like my interpretation of the spec is wrong and the question you pointed to is correct. The attribute without a prefix gets the default namespace of the unprefixed scope, not the namespace of the prefixed element. I ran some tests based on the other question and jelovirt is correct. – Samuel Neff Jul 30 '13 at 19:12
  • 1
    thanks for getting back on this - I was puzzling over it last night, as I ran some similar tests where xpaths to the prefix-less attributes weren't matching them. The spec seems to be rather ambiguous on this point (it would make sense for "interpretation of unprefixed attributes is determined by the element" to mean attributes _inherit_ the namespace) so I think our confusion is understandable! Interestingly this might mean the XmlSerializer is producing off-spec attributes... – Tryptamine42 Jul 31 '13 at 08:33
0

Try using the Prefix property?

XmlAttribute Prefix on MSDN

This is a rather odd problem, also setting the Prefix and NamespaceURI manually is probably error prone. Are you sure the namespace on the attribute is even necessary? While it may not be to spec, the client or server you are working with should skip the containing element of the attribute if it is in your foo namespace right? At which point why would it care what namespace your attribute has.

welegan
  • 3,013
  • 3
  • 15
  • 20
  • Thanks @welegan, sensible suggestions, it is indeed an odd problem. Hopefully I can drop InfoPath interoperation soon and so the need for the double prefixing of element and attribute will disappear. Directly modifying the attribute Prefix property in the DOM is the backup plan, but I'd like to avoid it if possible, as ideally I want all xml generation to be done by the serializer (amusingly when deserializing it doesn't care whether the attributes have a prefix or not - I'm sure that can't be right). – Tryptamine42 Jul 30 '13 at 16:43