首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析SOAP对象

解析SOAP对象
EN

Stack Overflow用户
提问于 2016-10-27 20:47:19
回答 1查看 150关注 0票数 1

我正在尝试将SOAP消息返回到自定义Deserialize的实例中,并且存在一些格式化问题。class是在由SvcUtil.exe生成的c#文件中定义的。

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

代码语言:javascript
复制
<?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>

我自己的尝试..。

代码语言:javascript
复制
<SOAP-ENV:Envelope 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" 
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:StartupNotification id="ref-1" 
    xmlns:a1="http://schemas.microsoft.com/clr/assem/MSGReceive%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <reboot_typeField>SOAP_REBOOT_POWERON</reboot_typeField>
    <xml_versionField id="ref-3">http://www.somedomain.com/xml/2013/05/17/SomeName.xsd</xml_versionField>
    <ExtendedNotificationBase_x002B_ip_addressField id="ref-4">192.168.1.11</ExtendedNotificationBase_x002B_ip_addressField>
    <ExtendedNotificationBase_x002B_firmware_versionField id="ref-5">2.2.2.2</ExtendedNotificationBase_x002B_firmware_versionField>
    <ExtendedNotificationBase_x002B_customer_versionField id="ref-6">unsupported feature</ExtendedNotificationBase_x002B_customer_versionField>
    <NotificationWithOSDState_x002B_osd_stateField>OSD_STATE_OK</NotificationWithOSDState_x002B_osd_stateField>
    <NotificationBase_x002B_mac_addressField id="ref-7">00:00:00:00:00:00</NotificationBase_x002B_mac_addressField>
    <NotificationBase_x002B_customer_IDField id="ref-8">SOMENAME0129</NotificationBase_x002B_customer_IDField>
    <NotificationBase_x002B_task_typeField>TASK_STARTUP</NotificationBase_x002B_task_typeField>
    <NotificationBase_x002B_notification_IDField>19</NotificationBase_x002B_notification_IDField>
    <NotificationBase_x002B_serverTask_IDField>0</NotificationBase_x002B_serverTask_IDField>
    <NotificationBase_x002B_callerType_IDField id="ref-9">SELF</NotificationBase_x002B_callerType_IDField>
    <NotificationBase_x002B_timestampField>2016-10-27T14:03:03.7532987-04:00</NotificationBase_x002B_timestampField>
</a1:StartupNotification>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

下面是我用来创建类实例的代码,我希望能够序列化/反序列化。现在,我只是尝试序列化自己的结果,并在视觉上将结果与从服务中得到的结果进行比较。

代码语言:javascript
复制
StartupNotification sn = new StartupNotification();

sn.xml_version = "http://www.somedomain.com/xml/2013/05/17/SomeName.xsd";
sn.reboot_type = RebootType.SOAP_REBOOT_POWERON;
sn.customer_version = "unsupported feature";
sn.firmware_version = "2.2.2.2";
sn.ip_address = "192.168.1.11";
sn.osd_state = OSD_State.OSD_STATE_OK;
sn.timestamp = DateTime.Now;
sn.callerType_ID = "SELF";
sn.serverTask_ID = 0;
sn.notification_ID = 19;
sn.task_type = TaskType.TASK_STARTUP;
sn.customer_ID = "SOMENAME0129";
sn.mac_address = "00:00:00:00:00:00";

NotificationContainer nc = new NotificationContainer();
nc.Item = sn;

SoapFormatter sf = new SoapFormatter();
MemoryStream ms = new MemoryStream();

sf.Serialize(ms, nc);

string output = Encoding.UTF8.GetString(ms.GetBuffer());
return output;

如果有人可以帮助我诊断为什么我的序列化对象与使用相同类的服务的输出不同的一些原因,这将是非常有帮助的。我只是在找一些线索去调查,或者我忽略了一些简单的东西。任何帮助都是感激的,谢谢!

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

代码语言:javascript
复制
/// <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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-28 18:50:14

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

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

编辑:忘了打字机,它不像描述所暗示的那样起作用。我确定的一种方法是使用少量手动编码来添加soap信封:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40293941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档