Tryptamine42
Tryptamine42

Reputation: 79

Xml Serialization to XmlAttribute: namespace prefix lost when Element in same namespace

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.

Upvotes: 2

Views: 3980

Answers (2)

Samuel Neff
Samuel Neff

Reputation: 74939

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

Upvotes: 1

welegan
welegan

Reputation: 3043

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.

Upvotes: 0

Related Questions