You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XML反序列化为C#类遇字段为空及额外字段适配问题求助

解决Salesforce Webhook XML反序列化问题

问题背景

给定Salesforce Webhook的Sample XML,使用现有C#类反序列化时,<sf:Id>字段值为空,同时需要适配Form_ID__c、Ticket_Id__c、Status这几个自定义字段。

修正后的C#类定义

using System.Xml.Serialization;

[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; }
}

[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 OrganizationId { get; set; }

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

    [XmlElement(ElementName = "SessionId")]
    public string 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; }
}

[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 = "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 Form_ID__c { get; set; }

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

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

    [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }
}

关键修正说明

  1. SessionId类型修正:原代码中SessionId类仅用于处理nil属性,但Sample XML里的<SessionId>是普通字符串值,因此直接将Notifications类中的SessionId属性改为string类型,删除多余的SessionId类定义。
  2. SObject字段命名空间匹配:<sf:Id>等字段属于urn:sobject.enterprise.soap.sforce.com命名空间,需为每个字段显式指定Namespace属性,确保反序列化时能正确匹配XML元素。
  3. 添加自定义字段:在SObject类中新增Form_ID__c、Ticket_Id__c、Status属性,并指定对应的命名空间。
  4. 保留xsi:type属性:为SObject类添加Type属性映射XML中的xsi:type,完整保留元数据信息。
  5. 移除冗余属性:删除Notifications类中多余的Xmlns属性,该命名空间已通过XmlRoot和XmlElement的Namespace属性声明。

反序列化示例代码

using System.IO;
using System.Xml.Serialization;

public static WebhookModelRequestBody DeserializeXml(string xmlContent)
{
    var serializer = new XmlSerializer(typeof(WebhookModelRequestBody));
    using (var reader = new StringReader(xmlContent))
    {
        return (WebhookModelRequestBody)serializer.Deserialize(reader);
    }
}

内容的提问来源于stack exchange,提问作者Prajwal Gunjal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 02:30:58