SelectSingleNode与XPath无法正常工作 - 适用于在线测试工具

时间:2017-07-28 22:16:21

标签: c# xml xpath

我有以下XML(只是其中的一部分,不能将其全部共享为机密):

<?xml version="1.0" encoding="utf-8"?>
<Report xsi:schemaLocation="Aircraft_SpecSheetDetails" Name="Aircraft_SpecSheetDetails" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Aircraft_SpecSheetDetails">
    <Aircraft_SpecSheet_SUB_SpecificData>
        <Report Name="Aircraft_SpecSheet_SUB_SpecificData">
            <table8 Textbox29="No">
                <Detail_Collection>
                    <Detail OperWeightType_Description="Maximum Landing Weight" AcrftOperWeight_Current_KG="43000" AcrftOperWeight_Current_LBS="94799" />
                    <Detail OperWeightType_Description="Maximum Take Off Weight" AcrftOperWeight_Current_KG="50300" AcrftOperWeight_Current_LBS="110893" />
                    <Detail OperWeightType_Description="Maximum Zero Fuel Weight" AcrftOperWeight_Current_KG="40800" AcrftOperWeight_Current_LBS="89949" />
                    <Detail OperWeightType_Description="Basic Empty Weight" AcrftOperWeight_Current_KG="27841" AcrftOperWeight_Current_LBS="61379" />
                </Detail_Collection>
            </table8>
        </Report>
    </Aircraft_SpecSheet_SUB_SpecificData>
</Report>

我想抓取Detail属性中具有特定值的OperWeightType_Description元素。

我的XPath看起来像这样://Detail[@OperWeightType_Description='Maximum Landing Weight']

使用此工具:https://www.freeformatter.com/xpath-tester.html

但是在C#中,下面的代码不起作用 - weightNode仍为NULL。

private int? ImportWeightValue(XmlNode report, string weightDescription)
{
    XmlNode weightNode = report.SelectSingleNode($"//Detail[@OperWeightType_Description='{weightDescription}']");

    if (weightNode != null)
    {
        return weightNode.ReadInt("AcrftOperWeight_Current_KG");
    }

    return null;
}

参数中的report XmlNodeDocumentElement - XML的<Report>元素。

0 个答案:

没有答案