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

.NET 7生成西班牙税务SOAP 1.1格式XML失败求助

问题诊断与解决方案

你的代码存在三个核心问题,导致无法生成符合要求的SOAP 1.1格式XML:

1. 未生成完整的SOAP信封结构

你手动添加了<AltaFactuSistemaFacturacion>元素,但没有让序列化器生成SOAP标准的<soapenv:Envelope>和<soapenv:Body>结构,这是SOAP请求的必要外层容器。

2. 序列化类型不匹配

你基于AltaFactuSistemaFacturacionRequest创建了序列化映射,但传入的是AltaFactuSistemaFacturacion实例,类型不匹配导致序列化内容偏离预期。WSDL生成的代理类中,请求参数通常会被包装在对应的Request类型中,比如AltaFactuSistemaFacturacionRequest会包含AltaFactuSistemaFacturacion属性。

3. 未配置命名空间前缀

目标XML使用了sum、sum1、soapenv等前缀,但你的代码没有指定这些前缀,导致生成的XML直接使用命名空间全称或默认前缀,不符合要求。


修改后的代码

private string Alta2XmlSoap(AltaFactuSistemaFacturacion envio)
{
    StringBuilder xml = new StringBuilder();
    // 配置XmlWriter,启用缩进并指定UTF-8编码
    var writerSettings = new XmlWriterSettings 
    { 
        Indent = true, 
        Encoding = Encoding.UTF8,
        OmitXmlDeclaration = false // 保留XML声明
    };

    using (XmlWriter writer = XmlWriter.Create(xml, writerSettings))
    {
        try
        {
            // 将数据包装为正确的请求类型
            var request = new AltaFactuSistemaFacturacionRequest 
            { 
                AltaFactuSistemaFacturacion = envio 
            };

            // 导入请求类型的SOAP映射
            XmlTypeMapping soapMapping = new SoapReflectionImporter().ImportTypeMapping(typeof(AltaFactuSistemaFacturacionRequest));
            XmlSerializer soapSerializer = new XmlSerializer(soapMapping);

            // 配置命名空间前缀,完全匹配目标格式
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
            namespaces.Add("sum", "https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/tike/cont/ws/SuministroLR.xsd");
            namespaces.Add("sum1", "https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/tike/cont/ws/SuministroInformacion.xsd");

            // 直接序列化请求,自动生成完整SOAP结构
            soapSerializer.Serialize(writer, request, namespaces);
        }
        catch (Exception ex)
        {
            // 不要吞掉异常,添加日志后重新抛出便于调试
            throw;
        }
    }
    return xml.ToString();
}

额外注意事项

  1. 日期格式修正:目标XML使用dd-MM-yyyy格式,你当前生成的是dd/MM/yyyy。需要在实体类的日期属性上添加特性,或自定义序列化逻辑:

    [XmlIgnore]
    public DateTime FechaExpedicionFacturaEmisor { get; set; }
    
    [XmlElement("FechaExpedicionFacturaEmisor")]
    public string FechaExpedicionFacturaEmisorString => FechaExpedicionFacturaEmisor.ToString("dd-MM-yyyy");
    
  2. 金额格式修正:目标XML使用无货币符号的小数格式(如0.4),你当前生成的是带欧元符号和逗号分隔的格式。确保属性类型为decimal,并移除格式化逻辑:

    public decimal CuotaRepercutida { get; set; }
    
  3. 验证代理类结构:确认Connected Services生成的AltaFactuSistemaFacturacionRequest类确实包含AltaFactuSistemaFacturacion属性,这对应WSDL中定义的操作参数结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:39:50