【问题标题】:DotNetCore WebAPI : problem parsing xml with namespace from PostmanDotNetCore WebAPI:使用 Postman 的命名空间解析 xml 的问题
【发布时间】:2019-05-06 23:53:37
【问题描述】:

我有一个来自第三方的请求数据,它是 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:Internal server error and failed。有人可以告诉我如何在 C# 中使用 Postman 的命名空间解析 xml。

【问题讨论】:

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


【解决方案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.NamespaceXmlElement.Namespace

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-16
    • 2012-08-28
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2014-01-10
    相关资源
    最近更新 更多