子元素上的 XML 命名空间 [英] XML namespace on child element

查看:29
本文介绍了子元素上的 XML 命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类结构:

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>

我应该如何放置 XML 属性来做到这一点?

How should I put XML attributes to do that?

推荐答案

没有一致的 XML 处理器会关心您的两种情况之间的差异,您也不应该关心.这就像属性的排序.这种差异在 XML 级别上是微不足道的.您必须下降到 XML 下方的文本级别才能检测或控制这种差异,但在 XML 级别您最好忽略它,因为它无关紧要.

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.

这篇关于子元素上的 XML 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆