首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中,如何反序列化完整的SOAP XML响应?

在C#中,如何反序列化完整的SOAP XML响应?
EN

Stack Overflow用户
提问于 2021-09-21 14:33:46
回答 1查看 353关注 0票数 1

我们需要打电话给SOAP服务。

不幸的是,WSDL生成的代码不能像预期的那样工作,因为它们需要MTOM哪个causes problems we have not been able to resolve。因此,我们被迫重新发明车轮,我试图找出最好的(或至少是一个好的方法)来做到这一点。

SOAP服务将创建如下响应:

代码语言:javascript
复制
--uuid:d49971de-e2c2-4af4-947d-c0acc0f6ad64
Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"
Content-Transfer-Encoding: binary
Content-ID: 

<root.message@cxf.apache.org>
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
        <soap:Body>
            <ns2:listAttachmentsResponse xmlns:ns2="http://ws.praxedo.com/v6/businessEvent">
                <return>
                    <resultCode>0</resultCode>
                    <entities>
                        <entityId>00044</entityId>
                        <name>JohnDoe.pdf</name>
                        <addedOnDevice>false</addedOnDevice>
                        <creationDate>2021-03-17T20:48:36.849+01:00</creationDate>
                        <external>false</external>
                        <id>1103057659_1500033662_C_05126a814b440b8c6efea0d195cbae8f</id>
                        <lastModificationDate>2021-05-04T20:38:42.829+02:00</lastModificationDate>
                        <size>249794</size>
                        <unmodifiable>false</unmodifiable>
                    </entities>
                    <entities>
                        <entityId>00044</entityId>
                        <name>OAY3PD.pdf</name>
                        <addedOnDevice>false</addedOnDevice>
                        <creationDate>2021-03-17T20:48:36.829+01:00</creationDate>
                        <external>false</external>
                        <id>1103057659_1500033662_C_04870c813b0c352bed5213425a946d5c</id>
                        <lastModificationDate>2021-05-04T20:38:42.829+02:00</lastModificationDate>
                        <size>465275</size>
                        <unmodifiable>false</unmodifiable>
                    </entities>
                </return>
            </ns2:listAttachmentsResponse>
        </soap:Body>
    </soap:Envelope>
--uuid:d49971de-e2c2-4af4-947d-c0acc0f6ad64--

  1. 是否有一种从响应中提取我们所关心的Xml的方便或惯用的方法,或者是否需要创建自定义逻辑来处理边界和内容元数据?

  1. 如何处理不同的名称空间和别名,以及缺少名称和名称的元素?

我希望结果是一个POCO,可能是这样的:

代码语言:javascript
复制
public class ListAttachmentsResponse {
    public int ResultCode { get; init; }
    public Attachment[] Attachments
}

public class Attachment {
    // This is the "entityId", we need to keep the leading zeros.
    public string BusinessEventId { get; init; }

    public string FileName { get; init; }
    public bool IsAddedOnDevice { get; init; }
    public DateTime CreationDate { get; init; }
    public bool IsExternal { get; init; }
    public string Id { get; init; }
    public DateTime LastModificationDate { get; init; }
    public int size { get; init; }
    public bool IsUnmodifiable { get; init ;}
}

..。但是如果其他的东西更容易检索的话,我可以灵活地处理这个结构。

EN

回答 1

Stack Overflow用户

发布于 2021-09-21 16:02:26

在使用一些示例数据使用Visual的“粘贴特殊->粘贴Xml作为类”之后,我能够获得以下方法:

代码语言:javascript
复制
        private const string EnvelopeClose = "</soap:Envelope>";

        private static listAttachmentsResponse Parse(string responseContent)
        {
            string data = responseContent[responseContent.IndexOf("<soap:Envelope")..];
            int envelopeCloseIndex = data.IndexOf(EnvelopeClose);
            data = data.Substring(0, envelopeCloseIndex) + EnvelopeClose;

            XmlSerializer serializer = new(typeof(ResponseEnvelope));
            ResponseEnvelope result;
            using (StringReader reader = new(data))
            {
                result = (ResponseEnvelope)serializer.Deserialize(reader);
            }
            return result.Body.listAttachmentsResponse;
        }

我认为这是非常丑陋的,所以我愿意接受更好的方法来做这件事!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69270797

复制
相关文章

相似问题

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