从多个类生成XML

时间:2011-04-01 10:13:36

标签: c# .net xml xsd xml-namespaces

我正在尝试使用xsd.exe工具生成的类生成XML文档。

所需的结构如下:

 <sh:StandardBusinessDocument xmlns:eanucc="urn:ean.ucc:2" xmlns:order="urn:ean.ucc:order:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader ../Schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 ../Schemas/OrderProxy.xsd" xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
  <sh:StandardBusinessDocumentHeader>
    <sh:HeaderVersion>2.2</sh:HeaderVersion>    
  </sh:StandardBusinessDocumentHeader>
  <eanucc:message>
    <entityIdentification>
        <uniqueCreatorIdentification>2222</uniqueCreatorIdentification>
    </entityIdentification> 
  </eanucc:message>
</sh:StandardBusinessDocument> 

但我只能做到这一点:

<sh:StandardBusinessDocument xmlns:eanucc="urn:ean.ucc:2" xmlns:order="urn:ean.ucc:order:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader ../Schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 ../Schemas/OrderProxy.xsd" xmlns:sh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
  <sh:StandardBusinessDocumentHeader>
    <sh:HeaderVersion>2.2</sh:HeaderVersion>    
  </sh:StandardBusinessDocumentHeader>
  <sh:message>
    <entityIdentification>
        <uniqueCreatorIdentification>2222</uniqueCreatorIdentification>
    </entityIdentification> 
  </sh:message>
</sh:StandardBusinessDocument> 

如何为根节点的子节点创建不同的节点前缀?在我生成的类中,我使用XmlTypeAttribute和XmlRootAttribute添加了名称空间,但第二个子属性忽略了这些名称空间。我的类具有以下属性:

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", IsNullable = true)]
public class StandardBusinessDocument
{
   private StandardBusinessDocumentStandardBusinessDocumentHeader standardBusinessDocumentHeaderField;

   private StandardBusinessDocumentMessage messageField;
} 

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader", IsNullable = false)]
public class StandardBusinessDocumentStandardBusinessDocumentHeader : StandardBusinessDocument
{
}

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:ean.ucc:2")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:ean.ucc:2", IsNullable = true)]
public class StandardBusinessDocumentMessage
{
}

为什么忽略第二个孩子(StandardBusinessDocumentMessage)上的装饰的想法?

1 个答案:

答案 0 :(得分:2)

首先,在映射中为检索messageField的属性指定命名空间(我假设有一个):

[XmlElement(Namespace = "urn:ean.ucc:2")]
public StandardBusinessDocumentMessage Message { get; set; }

然后,在序列化对象时,创建XmlSerializerNamespaces类的实例,向其添加前缀和名称空间,并在Serialize()重载中使用它,该重载将XmlSerializerNamespaces对象作为参数:

  XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  ns.Add("eanucc", "urn:ean.ucc:2");
  ns.Add("sh", "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader");
  XmlSerializer xs = new XmlSerializer(typeof(StandardBusinessDocument));
  xs.Serialize(someStream, someInstance, ns);