0

I have the following SOAP response and I want to get the value from the tag element a:Year1. How can I achieve this using c#?

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetPerformanceAndRiskByTypeCodeResponse xmlns="http://tempuri.org/">
      <GetPerformanceAndRiskByTypeCodeResult xmlns:a="http://schemas.datacontract.org/2004/07/FE.Toolkit.Services.DataContracts.ResponsiveChartingTool" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <IsSuccessful xmlns="http://schemas.datacontract.org/2004/07/FE.Toolkit.Services.DataContracts">false</IsSuccessful>
        <ResponseCode xmlns="http://schemas.datacontract.org/2004/07/FE.Toolkit.Services.DataContracts">0</ResponseCode>
        <ResponseMessage i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/FE.Toolkit.Services.DataContracts" />
        <TotalRows xmlns="http://schemas.datacontract.org/2004/07/FE.Toolkit.Services.DataContracts">0</TotalRows>
        <a:CalendarPerformanceAs>2019-12-31T00:00:00</a:CalendarPerformanceAs>
        <a:CumulativePerformanceAs>2020-10-01T00:00:00</a:CumulativePerformanceAs>
        <a:DiscretePerformanceAs>2020-09-30T00:00:00</a:DiscretePerformanceAs>
        <a:InstrumentInformation>
          <a:InstrumentInformation>
            <a:CalendarPerformance>
              <a:IncomeBasisPriceType>2</a:IncomeBasisPriceType>
              <a:PerformanceCurrency>USD</a:PerformanceCurrency>
              <a:Year1>19.031154</a:Year1>
              <a:Year2>-13.434495</a:Year2>
              <a:Year3>23.004020</a:Year3>
              <a:Year4>-5.584117</a:Year4>
              <a:Year5>-2.882996</a:Year5>
            </a:CalendarPerformance>
          </a:InstrumentInformation>
        </a:InstrumentInformation>
        <a:RiskAs>2020-09-30T00:00:00</a:RiskAs>
      </GetPerformanceAndRiskByTypeCodeResult>
    </GetPerformanceAndRiskByTypeCodeResponse>
  </s:Body>
</s:Envelope>

I tried the following but no luck:

var str = XElement.Parse(xml.Response.XmlResponse.ToString()); 
var result = str.Element("InstrumentInformation").Element("InstrumentInformation")[0].Element("CalendarPerformance").Element("Year1").Value; 
Console.WriteLine("RESULT" + result);

The error is

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
Denis S.
  • 139
  • 2
  • 12
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a [mre]. Please [edit] your question to add these details into it or we may not be able to help. – gunr2171 Oct 02 '20 at 13:57
  • Updated the question, thanks for the note – Denis S. Oct 02 '20 at 14:04
  • A NullReferenceException means that one of the objects you are accessing is null. For example, one of your `Element()` calls returns null because it doesn't find anything, and further calling `Element()` on that returned value will be an error. [More details on NREs](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – gunr2171 Oct 02 '20 at 14:07
  • Look at this question to see if it answers your problem (note that you'll need to use XML namespaces): https://stackoverflow.com/questions/12201822/read-soap-message-using-c-sharp (this is a dup target, but I already VtC'd) – gunr2171 Oct 02 '20 at 14:07

1 Answers1

1

You need to deal with the namespace of the element. You can ignore it and use the local name but it is more robust to include it:

XNamespace ns = "http://schemas.datacontract.org/2004/07/FE.Toolkit.Services.DataContracts.ResponsiveChartingTool";

var y1 = str.Descendants().Elements(ns + "Year1").FirstOrDefault();
Crowcoder
  • 9,566
  • 3
  • 30
  • 39