使用Array修改SOAP消息,以便可以使用WCF XMLSerializer对其进行序列化

时间:2018-10-03 11:23:58

标签: .net wcf soap xml-parsing xml-serialization

我有一个返回此响应的SOAP服务:

<Soap:Envelope xmlns:Soap="http://schemas.xmlsoap.org/soap/envelope/">
  <Soap:Body>
    <Read_Result xmlns="urn:microsoft-dynamics-schemas/page/salesorder">
      <SalesOrder>
        <Key>28;JAAAAACLAQAAAAJ7BDEAMAAwADE=6;4923601;10;SalesLines2;36;JQAAAACLAQAAAAJ7BDEAMAAwADEAAAAAhxAn6;4923840;36;JQAAAACLAQAAAAJ7BDEAMAAwADEAAAAAhyBO7;15066990;</Key>
        <No>1001</No>
        <Sell_to_Customer_No>01905899</Sell_to_Customer_No>
        <SalesLines>
          <Sales_Order_Line>
            <Key>36;JQAAAACLAQAAAAJ7BDEAMAAwADEAAAAAhxAn6;4923840;</Key>
            <Type>Item</Type>
            <FilteredTypeField>Item</FilteredTypeField>
            <No>1000</No>
            <Quantity>6</Quantity>            
          </Sales_Order_Line>
          <Sales_Order_Line>
            <Key>36;JQAAAACLAQAAAAJ7BDEAMAAwADEAAAAAhyBO7;15066990;</Key>
            <Type>Item</Type>
            <FilteredTypeField>Item</FilteredTypeField>
            <No>1150</No>
            <Quantity>5</Quantity>            
          </Sales_Order_Line>
        </SalesLines>
      </SalesOrder>
    </Read_Result>
  </Soap:Body>
</Soap:Envelope>

我已阅读WCF生成的代理类使用的XMLSerializer中存在一个限制:

Using nested complex types in SOAP Message - WCF XmlSerializer

https://social.msdn.microsoft.com/Forums/vstudio/en-US/3268fd25-4a1d-46af-82ad-edcdb555de69/limitations-of-xmlserializer-what-objects-cannot-be-serialized?forum=csharpgeneral

因此,我正在尝试使用添加MessageInspector的方法-读取SOAP请求并进行一些XML格式化(以便反序列化可以正常工作)-您可以在此链接下阅读有关它的信息。

SalesLines部分出现问题,因为我的WCF代理将其返回为空

关于在示例SOAP XML响应中究竟需要修改哪些内容以使其与WCF的XMLSerializer“兼容”的任何想法?

这是我计划使用的基本代码:

public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) 
{ 
    // Read reply payload 
    XmlDocument doc = new XmlDocument(); 
    MemoryStream ms = new MemoryStream(); 
    XmlWriter writer = XmlWriter.Create(ms); 
    reply.WriteMessage(writer); 
    writer.Flush(); 
    ms.Position = 0; 
    doc.Load(ms); 

    // Change logic 
    //ChangeMessage(doc); //Here I will do the required modifications

    // Write the reply payload 
    ms.SetLength(0); 
    writer = XmlWriter.Create(ms); 
    doc.WriteTo(writer); 
    writer.Flush(); 
    ms.Position = 0; 
    XmlReader reader = XmlReader.Create(ms); 
    reply = System.ServiceModel.Channels.Message.CreateMessage(reader, int.MaxValue, reply.Version); 
}

0 个答案:

没有答案