There are xml of the following structure:

<Заказ> <ДатаЗаказа>22.06.2016 8:10:55</ДатаЗаказа> <НомерЗаказа>666666666666666</НомерЗаказа> <ФИО>Мванова Ивана Ивановна</ФИО> <Возраст>50 л. 8 мес.</Возраст> <Пол>Ж</Пол> <Исследования> <Услуга> <Код>1990</Код> <Биоматериал>соскоб для посева</Биоматериал> <НомерИсследования>251088</НомерИсследования> </Услуга> </Исследования> </Заказ> <Заказ> <ДатаЗаказа>22.06.2016 8:20:05</ДатаЗаказа> <НомерЗаказа>666666666666666</НомерЗаказа> <ФИО>Мванова Ивана Ивановна</ФИО> <Возраст>28 л.</Возраст> <Беременность> <СрокОт>6</СрокОт> <СрокДо>7</СрокДо> </Беременность> <Пол>Ж</Пол> <Исследования> <Услуга> <Код>1905</Код> <Биоматериал>моча</Биоматериал> <НомерИсследования>250977</НомерИсследования> </Услуга> </Исследования> </Заказ> 

when reading nodes as follows:

 XElement codeElement = anElement.Element("Исследования").Element("Услуга").Element("Код"); XElement maleElement = anElement.Element("Пол"); XElement pregElement = anElement.Element("Беременность").Element("СрокОт"); XElement localElement = anElement.Element("Исследования").Element("Услуга").Element("Примечание"); 

swears at the line:

  anElement.Element("Исследования").Element("Услуга").Element("Примечание"); 
  • Additional Information: An object reference does not indicate an object instance. But! Does not swear at other designs, if it does not matter, and the problem is with this element, tell me, who knows what the problem may be?

Addition: And it works and does not fall:) and if you comment out the line with the pregnancy, it is also executed

  • I do not believe. Your code should have fallen in the last line, not in this one. - Pavel Mayorov
  • 1. In the first order there is no Pregnancy item; 2. The Service element does not contain the Note element - kmv
  • In a specific example, there is no Notes, two lines below is the verification of this element. And what, what is it missing in the example? Below is a check on the presence of these elements, and if it does not fit the conditions, then the necessary sequence of actions is chosen. - Madushko

1 answer 1

Everything explained the situation - first you need to check the presence of the Pregnancy node, then the presence of the nested element.