-3

How can I get value of the attribute "CONTRACT_TYPE" in the below XML. The xml is passed as a string argument to my program. I have tried multiple ways but could not achieve it.

<?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>
Vijay Swaroop
  • 17
  • 1
  • 11
  • Please show us what you've tried. –  Sep 12 '16 at 16:40
  • 2
    CONTRACT_TYPE is a node/element not an attribute. – Alex K. Sep 12 '16 at 16:41
  • 2
    `I have tried multiple ways using LINQ to xml but could not achieve it.` => awesome, so it won't be any trouble then for you to post a couple of those attempts along with where you got stuck and we (community) can point you in the right direction. – Igor Sep 12 '16 at 16:47
  • var at = XElement.Parse("XML").Attribute(""); if (at != null) Console.Write(at.Value); Console.ReadKey(); – Vijay Swaroop Sep 12 '16 at 16:51

1 Answers1

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

this should work

Timon Post
  • 2,779
  • 1
  • 17
  • 32