Xml 反序列化

问题描述 投票:0回答:1

我想将 xml 反序列化为 C# 类

这是我正在使用的课程

面临获取 null 的问题

<sf:Id>12345</sf:Id>
该字段还添加了一些额外的内容 字段如
Form_ID__c
Ticket_Id__c
Status
。 谁能帮我反序列化这个。

[XmlRoot(ElementName = "SessionId")]
public class SessionId
{
    [XmlAttribute(AttributeName = "nil")]
    public bool Nil { get; set; }
}

[XmlRoot(ElementName = "sObject", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
public class SObject
{
    [XmlElement(ElementName = "Id", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public string Id { get; set; }
}

[XmlRoot(ElementName = "Notification", Namespace = "http://soap.sforce.com/2005/09/outbound")]
public class Notification
{
    [XmlElement(ElementName = "Id", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public string Id { get; set; }

    [XmlElement(ElementName = "sObject", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public SObject SObject { get; set; }
}

[XmlRoot(ElementName = "notifications", Namespace = "http://soap.sforce.com/2005/09/outbound")]
public class Notifications
{
    [XmlElement(ElementName = "OrganizationId")]
    public string OrganizationId { get; set; }

    [XmlElement(ElementName = "ActionId")]
    public string ActionId { get; set; }

    [XmlElement(ElementName = "SessionId")]
    public SessionId SessionId { get; set; }

    [XmlElement(ElementName = "EnterpriseUrl")]
    public string EnterpriseUrl { get; set; }

    [XmlElement(ElementName = "PartnerUrl")]
    public string PartnerUrl { get; set; }

    [XmlElement(ElementName = "Notification")]
    public Notification Notification { get; set; }

    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
}

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
    [XmlElement(ElementName = "notifications", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public Notifications Notifications { get; set; }
}

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class WebhookModelRequestBody
{
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { get; set; }
}

示例 XML

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Body>
    <notifications xmlns="http://soap.sforce.com/2005/09/outbound">
        <OrganizationId>1234</OrganizationId>
        <ActionId>1234</ActionId>
        <SessionId>12222xyshaydfn</SessionId>
        <EnterpriseUrl>https://google.com</EnterpriseUrl>
        <PartnerUrl>https://google.com</PartnerUrl>
        <Notification>
            <Id>12345</Id>
            <sObject xsi:type="sf:Social_Post__c" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
                <sf:Id>12345</sf:Id>
                <sf:Form_ID__c>12345</sf:Form_ID__c>
                <sf:Ticket_Id__c>12345</sf:Ticket_Id__c>
                <sf:Status>Closed</sf:Status>
            </sObject>
        </Notification>
    </notifications>
</soapenv:Body>
 </soapenv:Envelope>

使用此示例 xml 任何人都可以帮助我将 xml 反序列化为 c# 类

c# xml serialization xml-parsing
1个回答
0
投票

好吧,事情是这样的:我不太了解如何使用 xsi,所以这可能仍然需要一些编辑。但要点是:当您定义 xsi 类型时,它必须是您使用它的类型的派生类。现在,在你的情况下,因为我不知道细节,所以我会绕过它。


    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "notifications", Namespace = "http://soap.sforce.com/2005/09/outbound")]
        public Notifications Notifications { get; set; }
    }

    [XmlRoot(ElementName = "notifications", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public class Notifications
    {
        [XmlElement(ElementName = "OrganizationId")]
        public string OrgID { get; set; }
        [XmlElement(ElementName = "ActionId")]
        public string ActID { get; set; }
        [XmlElement(ElementName = "SessionId")]
        public string SesID { get; set; }
        [XmlElement(ElementName = "EnterpriseUrl")]
        public string EnterUrl { get; set; }
        [XmlElement(ElementName = "PartnerUrl")]
        public string PartnerUrl { get; set; }
        [XmlElement(ElementName = "Notification")]
        public Notification Notification { get; set; }

    }

    [XmlRoot(ElementName = "Notification", Namespace = "http://soap.sforce.com/2005/09/outbound")]
    public class Notification
    {
        [XmlElement(ElementName = "Id")]
        public string Id { get; set; }
        [XmlElement(ElementName = "sObject")]
        public SObject SObject { get; set; }
    }

    [XmlInclude(typeof(SObjType))]
    [XmlRoot(ElementName = "sObject", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public class SObject
    {
        [XmlElement(ElementName = "Id", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
        public string Id { get; set; }
        [XmlElement(ElementName = "Form_ID__c", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
        public string FromId { get; set; }
        [XmlElement(ElementName = "Ticket_Id__c", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
        public string TicketId { get; set; }
        [XmlElement(ElementName = "Status", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
        public string Status { get; set; }
    }



    [XmlType(TypeName = "Social_Post__c", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public class SObjType: SObject
    {

    }

但是既然你打算将其保存在 sObject 类型中,我想知道为什么我们甚至需要定义 SocialPost 类型。现在这当然可能是由于来自原始的序列化。但由于我不知道你想要什么的更多细节,这是我能给你的最好的。但建议:由于 xsi 类型是社交帖子,我假设 sObject 将具有较少的属性。所以这个答案不应该被视为完全最终的,但它会给你现在想要的东西。

问题是我假设 sObject 应该是一个占位符类型,其他事物都从中派生,因此 Id、FormId 等可能会在 SocialPost 中定义。所以类似于

[XmlInclude(typeof(SocialPost))]
[XmlRoot(ElementName = "sObject", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
public class SObject
{
    
}



[XmlType(TypeName = "Social_Post__c", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
public class SocialPost: SObject
{
    [XmlElement(ElementName = "Id", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public string Id { get; set; }
    [XmlElement(ElementName = "Form_ID__c", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public string FromId { get; set; }
    [XmlElement(ElementName = "Ticket_Id__c", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public string TicketId { get; set; }
    [XmlElement(ElementName = "Status", Namespace = "urn:sobject.enterprise.soap.sforce.com")]
    public string Status { get; set; }
}

但是一旦像这样反序列化,您将拥有一个可以转换为 SocialPost 的 sObject 类型。

© www.soinside.com 2019 - 2024. All rights reserved.