XML namespace on child element

早过忘川 提交于 2020-01-16 07:59:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!