【问题标题】:C# .net 4.6 XmlSerializerNamespaces XmlSerializer output problemC# .net 4.6 XmlSerializerNamespaces XmlSerializer 输出问题
【发布时间】:2018-10-15 10:07:24
【问题描述】:

我需要以下从 Pojo 类到 Xml 的输出。 它是 .net 4.6.1,C#。 问题出在名称空间和生成的前缀中。 如何获取节点: <blablubb:Message SOAP-ENV:mustUnderstand="1" xmlns:blablubb="urn:blablubb-com:schemas:blablubb">

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <blablubb:Message SOAP-ENV:mustUnderstand="1" xmlns:blablubb="urn:blablubb-com:schemas:blablubb">
           <blablubb:Type>INET_TRANS</blablubb:Type>
           <blablubb:Function>RECEIVE_CUSTOMER_ORDER</blablubb:Function>
           <blablubb:Sender>CONNECT</blablubb:Sender>
           <blablubb:Receiver>CONNECT</blablubb:Receiver>
           <blablubb:SentAt>2018-08-30T10:43:37+02:00</blablubb:SentAt>
           <blablubb:ExpiresAt>2018-09-30T10:43:37+02:00</blablubb:ExpiresAt>
        </blablubb:Message>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body><ORDERS xmlns="urn:blablubb-com:schemas:inbound_distribution_transactions_create_customer_order_request" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <BUYER></BUYER>
 <CONTACT_REFERENCE>OAK</CONTACT_REFERENCE>
 <EAN_LOCATION_DELIVERY_ADDRESS>10901000</EAN_LOCATION_DELIVERY_ADDRESS>
 <CURRENCY_CODE>EUR</CURRENCY_CODE>
 <ORDER_DATE>2018-08-27T09:01:01</ORDER_DATE>
 <OUR_CUSTOMER_NO>2</OUR_CUSTOMER_NO>
 <SUPPLIER_ADDRESS_NO></SUPPLIER_ADDRESS_NO>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

被序列化的 Pojo:

肥皂文档:

 [Serializable]
 [XmlRoot("Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
 [XmlInclude(typeof(SoapHeader))]
 public class SoapDocument {
     [XmlElement("Header")]
     public SoapHeader soapHeader { get; set; }

     [XmlElement("Body",typeof(IfsOrdersXmlDocument))]
     public object soapBody { get; set; }
 }

肥皂头:

[Serializable]
[XmlRoot("Header")]
[XmlInclude(typeof(SoapMessage))] // include type class    IfsOrderLineXmlDocument
public class SoapHeader {
[XmlElement("Message", Namespace = "urn:blablubb-com:schemas:blablubb")]
public SoapMessage soapMessage { get; set; }
     }

肥皂体:

[Serializable]
[XmlInclude(typeof(IfsXmlDocument)),  XmlInclude(typeof(IfsOrdersXmlDocument))]
[SoapInclude(typeof(IfsXmlDocument)), SoapInclude(typeof(IfsOrdersXmlDocument))]
public class SoapBody {
[XmlElement("Body")]
public IfsXmlDocument soapBody { get; set; }
}

肥皂消息:

[Serializable]
//[XmlType("blablubb")]
[XmlRoot("Message", Namespace = "urn:blablubb-com:schemas:blablubb")]
public class SoapMessage {
[XmlElement("Type")]
public string Type { get; set; }
[XmlElement("Function")]
public string Function { get; set; }
[XmlElement("Sender")]
public string Sender { get; set; }
[XmlElement("Receiver")]
public string Receiver { get; set; }
[XmlElement("SentAt")]
public string SentAt { get; set; }
[XmlElement("ExpiresAt")]
public string ExpiresAt { get; set; }
}

生成 Xml 的代码, 所以问题出在命名空间和前缀上......

XmlSerializerNamespaces ns = createSoapDocument();
XmlSerializer serializer = new     XmlSerializer(typeof(HeinzApi.Soap.SoapDocument), new Type[] { typeof(IfsXmlDocument), typeof(IfsOrdersXmlDocument) });
FileStream fs = new FileStream(xmlTargetPath, FileMode.Create);
serializer.Serialize(fs, soapDocument, ns);
fs.Close();`

这就是我得到的:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header>
    <Message xmlns="urn:blablubb-com:schemas:blablubb">
      <Type>INET_TRANS</Type>
      <Function>RECEIVE_CUSTOMER_ORDER</Function>
      <Sender>CONNECT</Sender>
      <Receiver>CONNECT</Receiver>
      <SentAt>2018-01-03T10:43:37+02:00</SentAt>
      <ExpiresAt>2018-08-02T10:43:37+02:00</ExpiresAt>
    </Message>
  </SOAP-ENV:Header>
</SOAP-ENV:Envelope>

这是将命名空间添加到 XmlSerializer 命名空间对象的 createSoapDocument 方法:

private XmlSerializerNamespaces createSoapDocument() {
    soapMessage.Type = "INET_TRANS";
    soapMessage.Function = "RECEIVE_CUSTOMER_ORDER";
    soapMessage.Sender = "CONNECT";
    soapMessage.Receiver = "CONNECT";
    soapMessage.SentAt = "2018-01-03T10:43:37+02:00";
    soapMessage.ExpiresAt = "2018-08-02T10:43:37+02:00";

    soapHeader.soapMessage = soapMessage;
    soapDocument.soapHeader = soapHeader;
    //soapDocument.soapBody = (IfsXmlDocument)targetXmlBaseDocument; ;
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
    ns.Add("blablubb", "urn:blablubb-com:schemas:blablubb");
    return ns;
}

编辑:这是将命名空间添加到 XmlSerializerNamespaces 后的结果:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:blablubb="urn:blablubb-com:schemas:blablubb" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header>
    <blablubb:Message>
      <blablubb:Type>INET_TRANS</blablubb:Type>
      <blablubb:Function>RECEIVE_CUSTOMER_ORDER</blablubb:Function>
      <blablubb:Sender>CONNECT</blablubb:Sender>
      <blablubb:Receiver>CONNECT</blablubb:Receiver>
      <blablubb:SentAt>2018-01-03T10:43:37+02:00</blablubb:SentAt>
      <blablubb:ExpiresAt>2018-08-02T10:43:37+02:00</blablubb:ExpiresAt>
    </blablubb:Message>
  </SOAP-ENV:Header>
</SOAP-ENV:Envelope>

【问题讨论】:

    标签: c# xml serialization soap xmlserializer


    【解决方案1】:

    您需要通过将 blahblubb 命名空间添加到 XmlSerializerNamespaces 对象来告诉序列化程序。

    ns.Add("blablubb", "urn:bla-com:schemas:blablubb");
    

    别忘了改变

    [XmlElement("Body")]
    

    [XmlElement("Body", Namespace="urn:bla-com:schemas:blablubb")]
    

    【讨论】:

    • 这是我添加命名空间后得到的:&lt;?xml version="1.0"?&gt; &lt;SOAP-ENV:Envelope xmlns:fndcn="urn:ifsworld-com:schemas:fndcn" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;SOAP-ENV:Header&gt; &lt;fndcn:Message&gt; &lt;fndcn:Type&gt;INET_TRANS&lt;/fndcn:Type&gt; &lt;fndcn:Function&gt;RECEIVE_CUSTOMER_ORDER&lt;/fndcn:Function&gt; &lt;fndcn:Sender&gt;CONNECT&lt;/fndcn:Sender&gt; &lt;fndcn:Receiver&gt;CONNECT&lt;/fndcn:Receiver&gt; &lt;fndcn:SentAt&gt;2018-01-03T10:43:37+02:00&lt;/fndcn:SentAt&gt; &lt;fndcn:ExpiresAt&gt;2018-08-02T10:43:37+02:00&lt;/fndcn:ExpiresAt&gt;
    • 你能发布 createSoapDocument() 的代码吗?最好在原始问题中?
    • 我编辑了答案,请通过将 Namespace 属性添加到“Body”的 XmlElementAttribute 重试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2011-07-29
    相关资源
    最近更新 更多