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

C# WebMethod传递对象数组时Fila元素命名空间异常问题

解决方案

要解决<Fila>元素继承父级sga命名空间的问题,需通过显式配置XML序列化属性强制指定Fila的命名空间为空,同时保留parametros的正确命名空间绑定。具体操作如下:

1. 修改WebMethod的参数属性

给parametros参数添加XmlArray和XmlArrayItem属性:

  • XmlArray指定元素名parametros并绑定到sga命名空间
  • XmlArrayItem指定子元素名Fila并强制命名空间为空

修改后的WebMethod代码:

[WebMethod]
[SoapDocumentMethod(Action = "RecogerAlbaranes")]
[return: XmlElementAttribute("response")]
public XmlDocument RecogerAlbaranes(
    [XmlArray(ElementName = "parametros", Namespace = "urn:erp-ws-schemas/codeunit/SGA")]
    [XmlArrayItem(ElementName = "Fila", Namespace = "")]
    List<Fila> parametros
)
{
    // 方法逻辑
}

2. 调整Fila类的XML属性

将XmlRoot替换为XmlElement(XmlRoot仅对XML根节点生效,Fila作为嵌套子元素不会触发该属性),给每个属性显式指定空命名空间:

public class Fila
{
    [XmlElement(Namespace = "")]
    public string id { get; set; }
    // 其他属性需同理添加[XmlElement(Namespace = "")]
}

验证效果

修改后生成的SOAP请求将完全匹配目标格式:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sga="urn:erp-ws-schemas/codeunit/SGA" xmlns:rec="RecogerAlbaranes">    
<soapenv:Header/>    
<soapenv:Body>       
  <sga:RecogerAlbaranes>          
    <sga:parametros>             
      <Fila>                
        <id>0001</id>  
        ....               
      </Fila>             
      <Fila>                
        <id>0002</id> 
        ....                           
      </Fila>   
       .....                           
    </sga:parametros>       
  </sga:RecogerAlbaranes>    
</soapenv:Body> 
</soapenv:Envelope>

之前方案无效的原因

  • XmlRoot仅对作为XML根节点的类生效,Fila是嵌套子元素,该属性无法被序列化器识别
  • 包装类未配置正确的XmlArray/XmlArrayItem属性,导致生成多余的嵌套结构
  • SoapDocumentMethod仅控制SOAP动作和整体请求格式,无法直接修改子元素的命名空间

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 22:21:01