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; } }
关键修正说明
- SessionId类型修正:原代码中
SessionId类仅用于处理nil属性,但Sample XML里的<SessionId>是普通字符串值,因此直接将Notifications类中的SessionId属性改为string类型,删除多余的SessionId类定义。 - SObject字段命名空间匹配:
<sf:Id>等字段属于urn:sobject.enterprise.soap.sforce.com命名空间,需为每个字段显式指定Namespace属性,确保反序列化时能正确匹配XML元素。 - 添加自定义字段:在
SObject类中新增Form_ID__c、Ticket_Id__c、Status属性,并指定对应的命名空间。 - 保留xsi:type属性:为
SObject类添加Type属性映射XML中的xsi:type,完整保留元数据信息。 - 移除冗余属性:删除
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
相关产品推荐
相关产品推荐

