简体   繁体   English

子元素上的XML命名空间

[英]XML namespace on child element

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? 我应该如何设置XML属性呢?

No conformant XML processor will care about the difference between your two cases, and neither should you. 没有一致的XML处理器会关心你的两种情况之间的区别,你也不应该。 It's like ordering of attributes. 这就像属性的排序。 The difference is insignificant at the XML level. XML级别的差异是微不足道的。 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. 你必须放到XML下面的文本级别来检测或控制这种差异,但是在XML级别你最好忽略它,因为它无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM