c# - 在 WCF 服务中序列化 SOAP xml 节点时出现问题

标签 c# .net wcf

有人能帮我理解为什么当我收到一个 soap 信封时,当接收对象序列化 soap 主体时,我没有收到父节点内的所有后代节点吗?

合约接口(interface)类

    namespace AssemblyMDEPort
    {
        [ServiceContract(Name = "AssemblyMDEPort", Namespace = "urn:oasis:names:tc:legalxml:wsdl:WebServicesProfile-Definitions-4.0")]
        [XmlSerializerFormat]
        public interface IAssemblyMDEPort
        {
            [OperationContract(ReplyAction = "*", Action = "urn:oasis:names:tc:legalxml:wsdl:WebServicesProfile-Definitions-4.0/AssemblyMDEPort/NotifyCompleteRequest")]
            [XmlSerializerFormat(SupportFaults = true)]
            NotifyCompleteResponse NotifyReviewComplete(NotifyCompleteRequest request);
        }
    }

这是当 soap 信封到达我们的服务端点时执行的客户端 soap 接口(interface)方法。

 public NotifyCompleteResponse NotifyReviewComplete(NotifyCompleteRequest request)

这是我们的 SOAP 对象,用于处理传入 SOAP 信封的序列化。

[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class NotifyCompleteRequest  
{
    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "urn:oasis:names:tc:legalxml:wsdl:WebServicesProfile-Definitions-4.0", Order = 0)]
    public XElement NotifyCompleteRequestMessage;

    public NotifyFilingReviewCompleteRequest()
    {
    }
}

这是发送到服务中的 SOAP 信封示例

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:oasis:names:tc:legalxml:wsdl:WebServicesProfile-Definitions-4.0">
       <soapenv:Header/>
       <soapenv:Body>
            <NotifyCompleteRequestMessage xmlns="urn:oasis:names:tc:legalxml:wsdl:WebServicesProfile-Definitions-4.0">
    <SendingMDELocationID xmlns="urn:oasis:names:tc:legalxml:schema:xsd:CommonTypes-4.0">
        <IdentificationID xmlns="http://niem.gov/niem/niem-core/2.0"></IdentificationID>
    </SendingMDELocationID>
                <SendingMDEProfileCode xmlns="urn:oasis:names:tc:legalxml:schema:xsd:CommonTypes-4.0">urn:oasis:names:tc:legalxml:schema:xsd:WebServicesMessaging-2.0</SendingMDEProfileCode>
                <ReviewCallbackMessage xmlns="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:ReviewFilingCallbackMessage-4.0" xmlns:nc="http://niem.gov/niem/niem-core/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <nc:DocumentFiledDate>
                        <nc:DateTime>2018-06-07T13:55:56.0Z</nc:DateTime>
                    </nc:DocumentFiledDate>
                </ReviewCallbackMessage>
            </NotifyCompleteRequestMessage>         
       </soapenv:Body>
    </soapenv:Envelope>

NotifyCompleteRequestMessage XElement 属性只加载第一个节点,而不是它的所有后代,如下所示,我们需要在 NotifyCompleteRequestMessage 属性中加载信封主体中的所有元素节点。必须有一种方法来实现这一点。

<SendingMDELocationID xmlns="urn:oasis:names:tc:legalxml:schema:xsd:CommonTypes-4.0">
    <IdentificationID xmlns="http://niem.gov/niem/niem-core/2.0"></IdentificationID>
</SendingMDELocationID>

最佳答案

错误可能是由于命名空间 nc 没有被定义。用于测试更正错误。尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement notifyCompleteRequestMessage = doc.Descendants().Where(x => x.Name.LocalName == "NotifyCompleteRequestMessage").FirstOrDefault();
            XNamespace ns = notifyCompleteRequestMessage.GetDefaultNamespace();
            DateTime date = (DateTime)notifyCompleteRequestMessage.Descendants().Where(x => x.Name.LocalName == "DateTime").FirstOrDefault();
        }
    }
}

关于c# - 在 WCF 服务中序列化 SOAP xml 节点时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57469106/

相关文章:

c# - 如何重构此代码(C#,类型)

c# - 如何将此 C# lambda 表达式转换为 VB.Net?

c# - HttpListener 调用了两次

c# - WCF - 复杂对象 - 已知类型

wcf - 使用 WCF HTTPS 终结点从控制台应用程序调用 Service Fabric 服务

wcf - 从内存中插入和查询数据的最佳实践

c# - 创建FileStream进行网络共享非常慢

c# - 等待 WCF 服务的最佳方式?

c# - 使用依赖注入(inject)将 token 获取从startup.cs传递到HomeController.cs

c# - C# 中的 MemoryStream 导致 Xml 模式验证失败