Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析SOAP对象_C#_Xml_Soap - Fatal编程技术网

C# 解析SOAP对象

C# 解析SOAP对象,c#,xml,soap,C#,Xml,Soap,我正试图将一条SOAP消息反序列化为一个自定义类的实例,但遇到了一些格式问题。类在从SvcUtil.exe生成的c#文件中定义 我从服务收到的消息按以下方式格式化,但当我尝试从同一类的实例序列化我自己的消息时,它们看起来不同 <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-

我正试图将一条SOAP消息反序列化为一个自定义
类的实例,但遇到了一些格式问题。
在从
SvcUtil.exe
生成的c#文件中定义

我从服务收到的消息按以下方式格式化,但当我尝试从同一类的实例序列化我自己的消息时,它们看起来不同

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" 
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:sde1="http://www.somedomain.com/xml/SomeName/" 
xmlns:sde2="http://www.somedomain.com/xml/2013/05/17/SomeName.xsd">
<SOAP-ENV:Body>
    <sde2:notification_message>
        <sde2:startup_notification 
        xml_version="http://www.somedomain.com/xml/2013/05/17/SomeName.xsd" 
        reboot_type="SOAP_REBOOT_POWERON" 
        customer_version="unsupported feature" 
        firmware_version="2.2.2.2" 
        ip_address="192.168.1.11" 
        osd_state="OSD_STATE_OK" 
        timestamp="1970-01-01T00:09:00.048895+00:00" 
        callerType_ID="SELF" 
        serverTask_ID="0" 
        notification_ID="19" 
        task_type="TASK_STARTUP" 
        customer_ID="SOMENAME0129" 
        mac_address="00:00:00:00:00:00">
        </sde2:startup_notification>
    </sde2:notification_message>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如果有人能潜在地帮助我诊断为什么序列化对象看起来与使用相同类的服务的输出不同,这将非常有帮助。我只是在寻找一些线索,或者如果我忽略了一些简单的事情。感谢您的帮助,谢谢

编辑:添加有关StartupNotification的请求信息。这个类继承自其他3个层,但它们的格式都相同,只是添加了新的属性

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.somedomain.com/xml/2013/05/17/SomeName.xsd")]
public partial class StartupNotification : ExtendedNotificationBase {

    private RebootType reboot_typeField;

    private string xml_versionField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public RebootType reboot_type {
        get {
            return this.reboot_typeField;
        }
        set {
            this.reboot_typeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string xml_version {
        get {
            return this.xml_versionField;
        }
        set {
            this.xml_versionField = value;
        }
    }
}
//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“wsdl”,“2.0.50727.3038”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.somedomain.com/xml/2013/05/17/SomeName.xsd")]
公共部分类StartupNotification:ExtendedNotificationBase{
私有重新启动类型重新启动类型字段;
私有字符串xml_versionField;
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共重新启动类型重新启动类型{
得到{
返回此.reboot\u类型字段;
}
设置{
this.reboot_typeField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串xml_版本{
得到{
返回this.xml_version字段;
}
设置{
this.xml_versionField=值;
}
}
}

SoapFormatter旨在支持通道两端都使用.NET的远程处理场景中的RPC调用。它将向输出中添加一些关于程序集和类型的元数据

如果要手动创建SOAP消息,可以更好地使用XmlSerializer,并将类包装在类型映射器中,如descibed

编辑: 忘记typemapper吧,它并不像描述中所说的那样工作。我知道一种肯定有效的方法是使用少量手动编码添加soap信封:

XElement snElement;
XmlSerializer mySerializer = new XmlSerializer(typeof(StartupNotification));
using (MemoryStream ms = new MemoryStream())
{
     mySerializer.Serialize(ms, sn);
     ms.Position = 0;
     snElement = XElement.Load(ms);
}

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XElement soap = new XElement(soapenv + "Envelope",
new XElement(soapenv + "Body", snElement));
// use soap.ToString() to inspect the result

首先在根目录中获取正确的名称空间。您使用的是clr而不是sde1和sde2。能否为StartupNotification类添加生成的代码?看起来底层字段被序列化而不是属性。@WvanNoort我已经添加了请求的信息。当我使用XmlSerializer进行序列化时,它会正确地执行属性,但是使用SoapFormatter时,它们是错误的。这是否允许我将XML序列化对象包装在soap信封中?我是否需要手动添加特定于SOAP的属性,或者是否可以使用不同的属性重新生成从SvcUtil创建的类。。。我有点迷路了。
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.somedomain.com/xml/2013/05/17/SomeName.xsd")]
public partial class StartupNotification : ExtendedNotificationBase {

    private RebootType reboot_typeField;

    private string xml_versionField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public RebootType reboot_type {
        get {
            return this.reboot_typeField;
        }
        set {
            this.reboot_typeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string xml_version {
        get {
            return this.xml_versionField;
        }
        set {
            this.xml_versionField = value;
        }
    }
}
XElement snElement;
XmlSerializer mySerializer = new XmlSerializer(typeof(StartupNotification));
using (MemoryStream ms = new MemoryStream())
{
     mySerializer.Serialize(ms, sn);
     ms.Position = 0;
     snElement = XElement.Load(ms);
}

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XElement soap = new XElement(soapenv + "Envelope",
new XElement(soapenv + "Body", snElement));
// use soap.ToString() to inspect the result