none
XElement join throwing exception Object reference not set to an instance of an object. RRS feed

  • Question

  • i have a xml file which has many element called TickerBrokerDateFormatMaps & TickerBrokerDateFormatMap

    i am just joining two element by TickerBrokerDateFormatMaps_Id

    this is my sample xml

      <TickerBrokerDateFormatMaps BrokerTab_Id="0" Broker_Id="0" Ticker_Id="KDP">
        <TickerBrokerDateFormatMaps_Id>1</TickerBrokerDateFormatMaps_Id>
        <DateFormat>1Q,2Q,3Q,4Q,FY,SPACE</DateFormat>
        <dgvcollection_Row_index>0</dgvcollection_Row_index>
      </TickerBrokerDateFormatMaps>
      
      <TickerBrokerDateFormatMap BrokerTab_Id="1" Broker_Id="0" Ticker_Id="KDP">
        <StandardDate>2Q 2017A</StandardDate>
        <ColumnCoordinate>Q</ColumnCoordinate>
        <TickerBrokerDateFormatMaps_Id>1</TickerBrokerDateFormatMaps_Id>
        <BrokerDate StandardDate="2Q 2017A" Broker_Id="0" BrokerTab_Id="1">
          <year>2Q17 A</year>
          <Quater>2Q17 A</Quater>
        </BrokerDate>
      </TickerBrokerDateFormatMap>
      
      XElement AllDateMapElement = XElement.Load(pathTickerBroker);
      
      var query1 = (from s7 in AllDateMapElement.Elements("TickerBrokerDateFormatMaps")
    			 join s8 in AllDateMapElement.Elements("TickerBrokerDateFormatMap")
    			 on (string)s7.Element("TickerBrokerDateFormatMaps_Id") equals (string)s8.Element("TickerBrokerDateFormatMaps_Id")
    			 select new
    			 {
    				 Quater = Convert.ToString(s8.Element("BrokerDate").Element("Quater").Value),
    				 YearQuater = Convert.ToString(s8.Element("BrokerDate").Element("year").Value)
    			 }).ToList();
      

    I am trying to fetch value from nested element of  BrokerDate element and those elements are

    Quater & YearQuater but getting exception. is there any syntax wrong ?

    if i comment below two lines then not getting any exception.

     Quater = Convert.ToString(s8.Element("BrokerDate").Element("Quater").Value),
     YearQuater = Convert.ToString(s8.Element("BrokerDate").Element("year").Value)

     i guess this syntax is ok s8.Element("BrokerDate").Element("year").Value

     what wrong is there in my code for which i am getting error. please help. thanks

    Monday, December 2, 2019 1:58 PM

All replies

  • Hi Sudip_inn,
    I tested your code and didn't get the error.
    You should add a root level element that including TickerBrokerDateFormatMap and TickerBrokerDateFormatMaps element, because XML document can't contain multiple root level elements.
    Here is the content of XML document.
    <root>
     <TickerBrokerDateFormatMaps BrokerTab_Id="0" Broker_Id="0" Ticker_Id="KDP">
        <TickerBrokerDateFormatMaps_Id>1</TickerBrokerDateFormatMaps_Id>
        <DateFormat>1Q,2Q,3Q,4Q,FY,SPACE</DateFormat>
        <dgvcollection_Row_index>0</dgvcollection_Row_index>
      </TickerBrokerDateFormatMaps>
      <TickerBrokerDateFormatMap BrokerTab_Id="1" Broker_Id="0" Ticker_Id="KDP">
        <StandardDate>2Q 2017A</StandardDate>
        <ColumnCoordinate>Q</ColumnCoordinate>
        <TickerBrokerDateFormatMaps_Id>1</TickerBrokerDateFormatMaps_Id>
        <BrokerDate StandardDate="2Q 2017A" Broker_Id="0" BrokerTab_Id="1">
          <year>2Q17 A</year>
          <Quater>2Q17 A</Quater>
        </BrokerDate>
      </TickerBrokerDateFormatMap>
    </root>
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 3, 2019 7:53 AM