检索xml的属性值

本文关键字:属性 xml 检索 | 更新日期: 2023-09-27 18:06:44

如何在下面的XML中获得属性"CONTRACT_TYPE"的值。xml作为字符串参数传递给我的程序。我已经尝试了多种方法,但都没有成功。

<?xml version="1.0" encoding="UTF-8"?>
    <FX_CORPORATE>
      <TRADE>
        <REFERENCE_NUMBER>1188586</REFERENCE_NUMBER>
        <TRANSACTION_DATE>20160902</TRANSACTION_DATE>
        <CONTRACT_TYPE>FX-SPOT</CONTRACT_TYPE>
        <COUNTERPARTY>SRI IMEL</COUNTERPARTY>
        <CROSS>NO</CROSS>
        <VIA_CURRENCY>USD</VIA_CURRENCY>
        <TRADER>EMXMONAB</TRADER>
        <POSITION_CURRENCY>AUD</POSITION_CURRENCY>
        <VALUE_DATE>20160906</VALUE_DATE>
        <BUY>
          <CURRENCY>AUD</CURRENCY>
          <AMOUNT>35000</AMOUNT>
        </BUY>
        <SELL>
          <CURRENCY>USD</CURRENCY>
          <AMOUNT>26400.5</AMOUNT>
        </SELL>
        <QUOTE>
          <RATE>0.7543</RATE>
        </QUOTE>
        <BUY_CURRENCY_RATES>
          <SPOT_RATE>0.7546</SPOT_RATE>
        </BUY_CURRENCY_RATES>
        <SELL_CURRENCY_RATES>
          <SPOT_RATE>0.7546</SPOT_RATE>
        </SELL_CURRENCY_RATES>
      </TRADE>
    </FX_CORPORATE>

检索xml的属性值

_doc = new XmlDocument();
_doc.Load("//link to xml file");
XmlNode node = doc.SelectSingleNode("FX_CORPORATE/TRADE/CONTRACT_TYPE");
        string value = node.InnerText;

应该可以