Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 106.3k

Tracking Number of Element (Nods or Sub-Nods) in an XML file

Aug 24 2017 3:32 AM
Hello my Code masters,
         I beleive you are all doing fine and in good health. I am writing a program that will loop through an XML file and display the total count of the following element's in the file :
  1. Encounter
  2. ART_StartDate
  3. Lab_Reports
  4. Viral_Loads
  5. Regimen
and write out the results in excel, below is the code that i have tried:
 
  1. //string directoryPath = "D:/TempDir/";  
  2.         string directoryPath = Server.MapPath(string.Format("~/{0}/""XML"));  
  3.         string[] filePaths = Directory.GetFiles(directoryPath, "*.xml"); //, SearchOption.AllDirectories  
  4.         string fileContent = "";  
  5.   
  6.         /*========================================= 
  7.          * this section is to initialise the Excel header and 
  8.          * the workbook in turn the worksheet 
  9.          * */  
  10.         try  
  11.         {  
  12.             ExcelPackage ps = new ExcelPackage();  
  13.             ps.Workbook.Worksheets.Add("EMR-NDR");  
  14.             ExcelWorksheet ws = ps.Workbook.Worksheets[1];  
  15.             object missValue = System.Reflection.Missing.Value;  
  16.   
  17.             Session["filename"] ="";  
  18.   
  19.             ps.Workbook.Properties.Author = "Abraham Oaltubosun";  
  20.             ps.Workbook.Properties.Title = "EMR-NDR Data Extraction Analytics";  
  21.   
  22.             ExcelRange ChartRange = ws.Cells["A1:D1"];  
  23.             ws.Cells[1, 1].Value = "Fil Names";  
  24.             ws.Cells[1, 2].Value = "HIV Encounter";  
  25.             ws.Cells[1, 3].Value = "CD4";  
  26.             ws.Cells[1, 4].Value = "Viral Load";  
  27.             ws.Cells[1, 5].Value = "ART Regimen";  
  28.   
  29.             int t = 2;  
  30.             foreach (string file in filePaths)  
  31.             {  
  32.                 // Log Message read started    
  33.                 Session["filename"] = file;  
  34.   
  35.                 fileContent = File.ReadAllText(file).Trim();  
  36.                 if (fileContent.Length > 0)  
  37.                 {  
  38.                     XDocument xd = XDocument.Parse(fileContent);  
  39.                     XmlDocument xxd = new XmlDocument();  
  40.                      xxd.Load(file);  
  41.                     //int HIVEnCounter = xd.Descendants("HIVEncounter").Count();  
  42.                      int HIVEnCounter = 0;  
  43.                     ////int RegimenCounter = 0;  
  44.                     int RegimenCounter1 = 0;  
  45.                     int LaboratoryCounter=0;  
  46.                     int ViralLoadCounter = 0;  
  47.   
  48.                     string result = "";  
  49.   
  50.                     HIVEnCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/Encounters/HIVEncounter").Count;  
  51.   
  52.                     string result2 = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();  
  53.   
  54.                     if (!result2.Contains("Regimen") && !result2.Contains("Encounters"))  
  55.                     {  
  56.                         result =(string)xd.Element("Container").Element("IndividualReport").Element("Condition").Element("Regimen").Element("PrescribedRegimenTypeCode").Value;  
  57.                     }  
  58.                       
  59.                     if (!string.IsNullOrEmpty(result))  
  60.                     {  
  61.                         if (result == "ART")  
  62.                         {  
  63.                             #region  
  64.                             //RegimenCounter = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("Regimen").Element("PrescribedRegimenTypeCode").Attributes("ART").Count();  
  65.                             //RegimenCounter = xd.Descendants("Regimen").Count();  
  66.                             //RegimenCounter = xxd.SelectNodes("").Count;  
  67.                             #endregion  
  68.   
  69.                             RegimenCounter1 = xxd.SelectNodes("/Container/IndividualReport/Condition/Regimen/PrescribedRegimenTypeCode[. = \"ART\"]").Count;  
  70.                         }  
  71.                     }  
  72.   
  73.                     string result3 = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();  
  74.                     if (result3.Contains("LaboratoryReport") && result3.Contains("LaboratoryResultedTest"))    
  75.                     {  
  76.                         result = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("LaboratoryReport").Element("LaboratoryOrderAndResult").Element("LaboratoryResultedTest").Element("CodeDescTxt").Value;  
  77.                     }  
  78.   
  79.                     if (!string.IsNullOrEmpty(result))  
  80.                     {  
  81.                         if (result == "CD4")  
  82.                         {  
  83.                             LaboratoryCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"CD4\"]").Count;  
  84.                             //LaboratoryCounter = xd.Descendants("LaboratoryReport").Count();  
  85.                         }  
  86.                      }  
  87.   
  88.                     string resultVL = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();  
  89.                     //resultVL = xxd.SelectNodes  
  90.                     if (resultVL.Contains("LaboratoryReport") && resultVL.Contains("LaboratoryResultedTest"))  
  91.                     {  
  92.                         resultVL = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("LaboratoryReport").Element("LaboratoryOrderAndResult").Element("LaboratoryResultedTest").Element("CodeDescTxt").Value;  
  93.                     }  
  94.                     if (!string.IsNullOrEmpty(resultVL))  
  95.                     {  
  96.                         //if (resultVL == "Viral Load")  
  97.                         //{  
  98.                             ViralLoadCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"Viral Load\"]").Count;  
  99.                             //ViralLoadCounter = xd.Descendants("LaboratoryReport").Count();  
  100.                         //}  
  101.                     }  
  102.                     #region  
  103.                     //int CD4Counts = xd.Descendants("CD4").Count();  
  104.                     //int LaboratoryCounter = xd.Descendants("LaboratoryOrderAndResult").Count();  
  105.                     //var border = ws.Cells.Style.Border;  
  106.                     //border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; //ExcelBorderStyle.Thin;  
  107.                     #endregion  
  108.   
  109.                     ws.Cells[t, 1].Value = file;  
  110.                     ws.Cells[t, 2].Value = HIVEnCounter;  
  111.                     ws.Cells[t, 3].Value = LaboratoryCounter;  
  112.                     ws.Cells[t, 4].Value = ViralLoadCounter;  
  113.                     ws.Cells[t, 5].Value = RegimenCounter1;  
  114.                       
  115.                     t = t + 1;  
  116.                 }  
  117.                 else  
  118.                 {  
  119.                     // Log error File is blank    
  120.                 }  
  121.   
  122.             }  
  123.             //============ Download Excel file =====================  
  124.             //Generate A File with Random name  
  125.             Byte[] bin = ps.GetAsByteArray();  
  126.             string files = directoryPath + "\\EMR-NDR.xlsx";  
  127.             File.WriteAllBytes(files, bin);  
  128.             Response.ClearContent();  
  129.             Response.Buffer = true;  
  130.             Response.AddHeader("Content-Disposition"string.Format("attachment; filename={0}", files));  
  131.             Response.ContentType = "application/ms-excel";  
  132.             Response.WriteFile(files);  
  133.             Response.End();  
  134.         }  
  135.         catch (Exception ex) { webMessage.Show("Error in "+Session["filename"].ToString()+" :" + ex.Message); }  
  136.     }  
 when i run the application i am getting "Object not reference error" kindly assist me, if possible amend where necessary as this application is very crucial to my project analysis success.
 
 I also attached ziped sample xml files to be use for testing.
 
I appreciate any for of techincal assistance, Thank you all 

Attachment: GHOKIGWE_APR_AUG_.zip

Answers (1)