Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 107.8k

How to get values from multiple xml nodes

Nov 28 2016 10:33 AM
Hi my Code masters,
I need your help again please, i have this XML file below
  1. <LaboratoryReport>  
  2.      <VisitID>103079VisitID>  
  3.      <VisitDate>2015-06-23VisitDate>   
  4.      <ARTStatusCode>AARTStatusCode>   
  5.      <LaboratoryOrderAndResult>   
  6.       <OrderedTestDate>2015-06-23OrderedTestDate>   
  7.       <LaboratoryResultedTest>   
  8.        <Code>11Code>   
  9.        <CodeDescTxt>CD4CodeDescTxt>   
  10.       LaboratoryResultedTest>   
  11.       <LaboratoryResult>   
  12.        <AnswerNumeric>   
  13.         <Value1>420Value1>   
  14.        AnswerNumeric>   
  15.       LaboratoryResult>   
  16.       <ResultedTestDate>2015-07-08ResultedTestDate>   
  17.      LaboratoryOrderAndResult>  
  18.      <LaboratoryOrderAndResult>   
  19.       <OrderedTestDate>2015-06-23OrderedTestDate>   
  20.       <LaboratoryResultedTest>   
  21.        <Code>46Code>   
  22.        <CodeDescTxt>HIV Rapid TestCodeDescTxt>   
  23.       LaboratoryResultedTest>   
  24.       <LaboratoryResult>   
  25.        <AnswerNumeric>   
  26.         <Value1>1Value1>   
  27.        AnswerNumeric>   
  28.       LaboratoryResult>   
  29.       <ResultedTestDate>2015-07-08ResultedTestDate>   
  30.      LaboratoryOrderAndResult>  
  31.    LaboratoryReport>  
  32.    <LaboratoryReport>  
  33.      <VisitID>107337VisitID>  
  34.      <VisitDate>2016-05-24VisitDate>   
  35.      <ARTStatusCode>AARTStatusCode>   
  36.      <LaboratoryOrderAndResult>   
  37.       <OrderedTestDate>2016-05-24OrderedTestDate>   
  38.       <LaboratoryResultedTest>   
  39.        <Code>80Code>   
  40.        <CodeDescTxt>Viral LoadCodeDescTxt>   
  41.       LaboratoryResultedTest>   
  42.       <LaboratoryResult>   
  43.        <AnswerNumeric>   
  44.         <Value1>18732Value1>   
  45.        AnswerNumeric>   
  46.       LaboratoryResult>   
  47.       <ResultedTestDate>2016-07-23ResultedTestDate>   
  48.      LaboratoryOrderAndResult>  
  49.    LaboratoryReport>  
i want to check if any of the element contain LaboratoryReport and LaboratoryResultedTest it should count the element that have it value equal to CD4.
this should also happend for any element that have it value equal to Viral Load
the code is working well for CD4 but it is not counting for Viral Load since the CD4 is the first element group.
my code is shown bellow :
  1. string directoryPath = Server.MapPath(string.Format("~/{0}/""XML"));  
  2.         string[] filePaths = Directory.GetFiles(directoryPath, "*.xml"); //, SearchOption.AllDirectories  
  3.         string fileContent = "";  
  4.   
  5.         /*========================================= 
  6.          * this section is to initialise the Excel header and 
  7.          * the workbook in turn the worksheet 
  8.          * */  
  9.             ExcelPackage ps = new ExcelPackage();  
  10.             ps.Workbook.Worksheets.Add("EMR-NDR");  
  11.             ExcelWorksheet ws = ps.Workbook.Worksheets[1];  
  12.             object missValue = System.Reflection.Missing.Value;  
  13.   
  14.             ps.Workbook.Properties.Author = "Abraham Oaltubosun";  
  15.             ps.Workbook.Properties.Title = "EMR-NDR Data Extraction Analytics";  
  16.   
  17.             ExcelRange ChartRange = ws.Cells["A1:D1"];  
  18.             ws.Cells[1, 1].Value = "Fil Names";  
  19.             ws.Cells[1, 2].Value = "HIV Encounter";  
  20.             ws.Cells[1, 3].Value = "CD4";  
  21.             ws.Cells[1, 4].Value = "Viral Load";  
  22.             ws.Cells[1, 5].Value = "ART Regimen";  
  23.   
  24. int t = 2;  
  25. foreach (string file in filePaths)  
  26. {  
  27. // Log Message read started    
  28. fileContent = File.ReadAllText(file).Trim();  
  29. if (fileContent.Length > 0)  
  30. {  
  31. XDocument xd = XDocument.Parse(fileContent);  
  32. XmlDocument xxd = new XmlDocument();  
  33.  xxd.Load(file);  
  34. int RegimenCounter = 0;  
  35. int RegimenCounter1 = 0;  
  36. int LaboratoryCounter=0;  
  37. int ViralLoadCounter = 0;  
  38.   
  39. string result = "";  
  40.   
  41. HIVEnCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/Encounters/HIVEncounter").Count;  
  42.   
  43.  string result2 = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();  
  44. if (result2.Contains("Regimen"))  
  45. {  
  46. result = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("Regimen").Element("PrescribedRegimenTypeCode").Value;  
  47. }  
  48.                       
  49. if (!string.IsNullOrEmpty(result))  
  50. {  
  51. if (result == "ART")  
  52. {  
  53. RegimenCounter1 = xxd.SelectNodes("/Container/IndividualReport/Condition/Regimen/PrescribedRegimenTypeCode[. = \"ART\"]").Count;  
  54. }  
  55. }  
  56.   
  57. string result3 = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();  
  58. if (result3.Contains("LaboratoryReport") && result3.Contains("LaboratoryResultedTest"))    
  59. {  
  60. result = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("LaboratoryReport").Element("LaboratoryOrderAndResult").Element("LaboratoryResultedTest").Element("CodeDescTxt").Value;  
  61. }  
  62.   
  63. if (!string.IsNullOrEmpty(result))  
  64. {  
  65. if (result == "CD4")  
  66. {  
  67. LaboratoryCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"CD4\"]").Count;  
  68.   
  69. }  
  70.  }  
  71.   
  72. string resultVL = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();  
  73.   
  74.  if (resultVL.Contains("LaboratoryReport") && resultVL.Contains("LaboratoryResultedTest"))  
  75. {  
  76.  resultVL = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("LaboratoryReport").Element("LaboratoryOrderAndResult").Element("LaboratoryResultedTest").Element("CodeDescTxt").Value;  
  77.                     }  
  78. if (!string.IsNullOrEmpty(resultVL))  
  79. {  
  80. if (resultVL == "Viral Load")  
  81. {  
  82. ViralLoadCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"Viral Load\"]").Count;  
  83. }  
  84. }  
  85.                   
  86. ws.Cells[t, 1].Value = file;  
  87. ws.Cells[t, 2].Value = HIVEnCounter;  
  88. ws.Cells[t, 3].Value = LaboratoryCounter;  
  89. ws.Cells[t, 4].Value = ViralLoadCounter;  
  90. ws.Cells[t, 5].Value = RegimenCounter1;   
  91. t = t + 1;  
  92. }  
  93. else  
  94. {  
  95.        // Log error File is blank    
  96. }  
  97.   
  98. }  
 please i need your help
 
Thank you all 

Answers (2)