简体   繁体   English

Xml序列化:无法在父元素和子元素中定义相同的名称空间

[英]Xml Serialization: Can't define same namespace in parent and child element

i need to create a xml structure similar to this: 我需要创建一个类似于以下内容的xml结构:

<?xml version="1.0" encoding="utf-8"?>
<ns0:RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://fuu.gub">
  <ns0:ChildElement xmlns:ns0="http://fuu.gub">
    <ns0:Data>Some-data</ns0:Data>
  </ns0:ChildElement>
</ns0:RootElement>

The namespace ns0 must be defined in the RootElement and in the ChildElement. 命名空间ns0必须在RootElement和ChildElement中定义。 I'm using Xml.Serialization to serialize my objects and i cant get this done. 我正在使用Xml.Serialization序列化我的对象,但我无法做到这一点。 The serializer is ignoring the namespace on the child element because it's already defined in the parent. 序列化程序将忽略子元素上的名称空间,因为它已在父元素中定义。 This means i end up with the following result: 这意味着我最终得到以下结果:

<?xml version="1.0" encoding="utf-8"?>
<ns0:RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://fuu.gub">
  <ns0:ChildElement>
    <ns0:Data>Some-data</ns0:Data>
  </ns0:ChildElement>
</ns0:RootElement>

Here is my code 这是我的代码

[XmlRoot("RootElement", Namespace = "http://fuu.gub")]
public class RootElement
{
    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces XmlNamespaces { get; set; }

    [XmlElement("ChildElement")]
    public ChildElement Child { get; set; }

    public RootElement() {
        XmlNamespaces = new XmlSerializerNamespaces();
        XmlNamespaces.Add("ns0", "http://fuu.gub");

        Child = new ChildElement();
    }

    public void ToXml(string path)
    {
        XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.GetType());

        TextWriter txtW = new StreamWriter(path);
        x.Serialize(txtW, this);
    }
}


[XmlRoot(Namespace = "http://fuu.gub")]
public class ChildElement
{
    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces XmlNamespaces { get; set; }

    [XmlElement]
    public string Data{ get; set; }

    public ChildElement()
    {
        XmlNamespaces = new XmlSerializerNamespaces();
        XmlNamespaces.Add("ns0", "http://fuu.gub");

        Data = "Some-data";
    }
}

Here http://www.w3schools.com/xml/xml_namespaces.asp I found this: 在这里http://www.w3schools.com/xml/xml_namespaces.asp我发现了这一点:

When a namespace is defined for an element, all child elements with the same prefix are associated with the same namespace. 为元素定义名称空间时,所有具有相同前缀的子元素都将与同一名称空间相关联。

Namespaces can be declared in the elements where they are used or in the XML root element 命名空间可以在使用它们的元素中或在XML根元素中声明。

Note: The namespace URI is not used by the parser to look up information. 注意:解析器不使用名称空间URI来查找信息。

When you define a namespace, you can´t use the same prefix to define another/the same namespace again. 定义名称空间时,不能使用相同的前缀来再次定义另一个/相同的名称空间。

I believe you can use the same namespace with a different prefix though. 我相信您可以使用带有不同前缀的相同名称空间。

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

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