选择SoapException.Detail.InnerXML的子元素

时间:2016-08-09 15:29:00

标签: c# web-services xpath soap

我正在调用UPS网络服务来计算运费,如果地址无效,服务调用将返回SoapException。

SoapException有一个Detail.InnerXMl属性,其中包含以下XML:

<err:Errors xmlns:err=\"http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1\">
    <err:ErrorDetail>
        <err:Severity>Hard</err:Severity>
        <err:PrimaryErrorCode>
            <err:Code>111286</err:Code>
            <err:Description>CAa is not a valid state for the specified shipment.</err:Description>
        </err:PrimaryErrorCode>
    </err:ErrorDetail>
</err:Errors>

我希望从<Description>元素中提取文本。

为什么这不起作用:

try
{
}
catch (SoapException ex)
{
    XmlNode node = ex.Detail.SelectSingleNode("//Description");
    //node is null
}

1 个答案:

答案 0 :(得分:0)

使用LinqToXml轻松解析xml:

class ItemGeneral(UpdateView):
    form_class = ItemUpdateForm
    template_name = 'pretixcontrol/item/index.html'
    permission = 'can_change_items'

    @transaction.atomic()
    def form_valid(self, form):
        # some code
        return super().form_valid(form)
相关问题