DotNetCore WebAPI:使用Postman中的命名空间解析xml时出现问题

时间:2018-12-05 12:00:29

标签: c# xml postman asp.net-core-webapi

我有一个从第三方获取的请求数据,该数据是XML。我想将此数据从邮递员传递到C#。

请求XML如下:

<n0:MassMatchQCResponseMsg xmlns:prx="urn:sap.com:proxy:TST:/1SAI/TASF64A312341D275609721:740" xmlns:n0="http://example.com/compxref/abctype">
    <MassMatchDet>
        <InputProductNumber>456141</InputProductNumber>        
    </MassMatchDet>    
</n0:MassMatchQCResponseMsg>

在这里,如果我删除命名空间别名n0并发布XML,则可以使用以下C#方法正常工作。

[HttpPost]
public IActionResult Post([FromBody]MassMatchQCResponseMsg value)
{

}

,但为n0,其显示状态为500:内部服务器错误,并且失败。有人可以告诉我如何在C#中使用Postman中的命名空间解析xml。

1 个答案:

答案 0 :(得分:1)

听起来好像您的MassMatchQCResponseMsg没有配置Namespace

[XmlRoot(Namespace="http://example.com/compxref/abctype")]
public class MassMatchQCResponseMsg{

    [XmlElement(Namespace="")]
    public MassMatchDet MassMatchDet {get;set;}
}

public class MassMatchDet
{
    public string InputProductNumber {get;set;}
}

注意 XmlRoot.Namespace XmlElement.Namespace

我使用上面的代码进行测试,它对我有用。

相关问题