开发者

Generate XML from multiple classes

开发者 https://www.devze.com 2023-02-21 01:27 出处:网络
I\'m trying to generate an XML document using a class generated by the xsd.exe tool. The desired structure is like this:

I'm trying to generate an XML document using a class generated by the xsd.exe tool.

The desired structure is like this:

 <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> 

But I have only been able to achieve this:

<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> 

How can I create the differing node prefixes for the children of the root node? In my generated class I have added the namespaces using the XmlTypeAttribute and XmlRootAttribute but these are ignored for the second child attribute. My classes have the attributes as below:

[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 StandardBu开发者_运维知识库sinessDocumentMessage 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
{
}

Any ideas why the decoration on the second child (StandardBusinessDocumentMessage) is ignored?


First, specify the namespace in the mapping for the property that retrieves messageField (I assume there is one):

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

Then, when serializing the object, create an instance of the XmlSerializerNamespaces class, add the prefixes and namespaces to it, and use it in the Serialize() overload that takes a XmlSerializerNamespaces object as a parameter:

  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);
0

精彩评论

暂无评论...
验证码 换一张
取 消