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

C#反序列化XML字符串为对象报错,求排查修正方案

XML反序列化C#对象错误排查与修正

错误原因解析

报错Cannot serialize member 'ccris_identity' of type Test.Domain.Business.Entities.GenCrfeidCCRIS_Identity. XmlAttribute/XmlText cannot be used to encode complex types.的核心问题:

  • 错误使用[XmlAttribute]标记复杂类型子元素:XmlAttribute仅适用于XML节点的属性(如<xml attr="value">),而你的XML结构里ccris_identity、spga_identity、item以及其下的字段都是子元素,不是属性,不能用XmlAttribute标记。
  • 字段大小写不匹配:实体类中GenCrfeIDSPGA_Identity的IDCode字段,和XML里的<IdCode>节点大小写不一致,反序列化时会无法完成映射。

修正后的实体类代码

[XmlRoot("xml")]
public class GenCrfeidData
{
    // 用XmlElement标记子元素,对应XML中的<ccris_identity>节点
    [XmlElement("ccris_identity")]
    public GenCrfeidCCRIS_Identity ccris_identity { get; set; }
    
    [XmlElement("spga_identity")]
    public GenCrfeIDSPGA_Identity spga_identity { get; set; }
}

public class GenCrfeidCCRIS_Identity
{
    // 用XmlElement标记列表项,对应多个<item>子元素
    [XmlElement("item")]
    public List<GenCrfeIDItem> item { get; set; }
}

public class GenCrfeIDItem
{
    // 所有字段都是XML子元素,用XmlElement标记
    [XmlElement("CrefId")]
    public string CRefId { get; set; }

    [XmlElement("EntityKey")]
    public string EntityKey { get; set; }

    [XmlElement("EntityId")]
    public string EntityId { get; set; }

    [XmlElement("EntityId2")]
    public string EntityId2 { get; set; }

    [XmlElement("EntityName")]
    public string EntityName { get; set; }

    [XmlElement("EntityDOBDOC")]
    public string EntityDOBDOC { get; set; }

    [XmlElement("EntityGroupCode")]
    public string EntityGroupCode { get; set; }

    [XmlElement("EntityState")]
    public string EntityState { get; set; }

    [XmlElement("EntityNationality")]
    public string EntityNationality { get; set; }

    [XmlElement("CcrisNote")]
    public string CcrisNote { get; set; }
}

public class GenCrfeIDSPGA_Identity
{
    [XmlElement("SRefId")]
    public string SRefId { get; set; }

    [XmlElement("Name")]
    public string Name { get; set; }

    // 修正大小写,匹配XML中的<IdCode>
    [XmlElement("IdCode")]
    public string IDCode { get; set; }

    [XmlElement("SpgaId")]
    public string SpgaId { get; set; }

    [XmlElement("Hit")]
    public string Hit { get; set; }
}

反序列化代码(无需修改,保持原逻辑即可)

string testData = @"<xml>
                                        <ccris_identity>
                                            <item>
                                                <CrefId></CrefId>
                                                <EntityKey></EntityKey>
                                                <EntityId></EntityId>
                                                <EntityId2/>
                                                <EntityName></EntityName>
                                                <EntityDOBDOC></EntityDOBDOC>
                                                <EntityGroupCode></EntityGroupCode>
                                                <EntityState/>
                                                <EntityNationality></EntityNationality>
                                                <CcrisNote/>
                                            </item>
                                        </ccris_identity>
                                        <spga_identity>
                                            <SRefId></SRefId>
                                            <Name></Name>
                                            <IdCode></IdCode>
                                            <SpgaId></SpgaId>
                                            <Hit></Hit>
                                        </spga_identity>
                                    </xml>";

XmlSerializer serializer = new XmlSerializer(typeof(GenCrfeidData));
using (StringReader reader = new StringReader(testData))
{
    var result = (GenCrfeidData)serializer.Deserialize(reader);
}

内容的提问来源于stack exchange,提问作者Fikri Halim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 18:25:39