问题
I've got the following class structure:
public class Child { ... }
[XmlRoot("parent", Namespace="parentNamespace")]
public class Parent
{
[XmlElement(Namespace="childNamespace")]
public Child Child { get; set; }
}
The I try to serialize it using namespaces:
namespaces.Add(string.Empty, "parentNamespace");
namespaces.Add("c", "childNamespace");
And I got the child namespace declared in parent element:
<parent xmlns:c="childNamespace" xmlns="parentNamespace">
<c:Child ... />
</parent>
But I want to move child namespace declaration to child element. Like this:
<parent xmlns="parentNamespace">
<c:Child ... xmlns:c="childNamespace">
</parent>
How should I put XML attributes to do that?
回答1:
No conformant XML processor will care about the difference between your two cases, and neither should you. It's like ordering of attributes. The difference is insignificant at the XML level. You'd have to drop to the text level beneath XML to detect or control such a difference, but at the XML level you're better off ignoring it because it does not matter.
来源:https://stackoverflow.com/questions/35368858/xml-namespace-on-child-element