Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 113.6k

How To Get The Value of XDocument Using C#

Nov 24 2016 5:34 AM
Hi My Code Masters,
 
Trust you all are doing just fine.
 Please I  want to get the Values of this Element <PrescribedRegimenTypeCode>ART</PrescribedRegimenTypeCode> from my XML files if the vale is ART i will then count Regimen else i will not. so i use this code
 
  1. int RegimenCounter = 0;    
  2. string result = "";    
  3. if(!string.IsNullOrEmpty(result=xd.Descendants("PrescribedRegimenTypeCode").Single().Value))    
  4. {    
  5.     if (result == "ART")    
  6.     {    
  7.       RegimenCounter = xd.Descendants("Regimen").Count();    
  8.     }    
  9. }  
 
when i execute the code i am getting an exception error "Sequence Contain no Element". it because the first XML file does not contain that Element "PrescribedRegimenTypeCode" while other files have in them.
please i need your help to resolve this challenge.
thank you
 
 
 
 
 
 
 

Answers (5)