使用xpath / c#从XML属性中提取值#

时间:2017-07-28 12:46:12

标签: c# xml xpath ssis

我目前正致力于在SSIS中的脚本任务中使用xpath和C#从XML文件中提取数据。 我想稍后将此信息填充到SQL表中。

我的输入Xml文件看起来像这样

<Order>
<Header dateOfExecution="2017-06-22 08:30:09" orderId="5000206348" status="O" messageId="1" type="REQ" serviceProviderId="SP010" externalId1="b0ddcfece1a345338f20902401fa1e71" />
<Body>
    <Oli>
        <OliControl oliId="1" subscriptionId="990448" />
        <MIGOPT>
            <MigratedOptions>
                <Option operation="CHG" optionType="FLNDetails" optionId="O2O0056">
                    <Attribute name="fixedLineOption" value="2" />
                    <Attribute name="portingDate" value="2017-07-03 06:00:00" />
                    <Attribute name="portingWindow" value="06:00:00" />
                    <Attribute name="fixedLineSource" value="D001" />
                    <Attribute name="fixedLineType" value="Analog" />
                    <Attribute name="fixedLineNumber" value="490" />
                    <Attribute name="LAC" value="06736" />
                </Option>
            </MigratedOptions>
        </MIGOPT>
    </Oli>
</Body>
</Order>

我设法获得&#34;类型&#34;的价值。从标题

 string  type = doc.SelectSingleNode("//Header/@type").InnerText;

但是当我试图从每个属性中获取值时,某些功能不起作用

 string portingDate = doc.SelectSingleNode("//Attribute[name='portingDate']/@value").InnerText;

我的错误在哪里?

1 个答案:

答案 0 :(得分:3)

@之前你遗漏了name。改变它:

doc.SelectSingleNode("//Attribute[@name='portingDate']/@value");