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 :
- Encounter
- ART_StartDate
- Lab_Reports
- Viral_Loads
- Regimen
and write out the results in excel, below is the code that i have tried:
-
- string directoryPath = Server.MapPath(string.Format("~/{0}/", "XML"));
- string[] filePaths = Directory.GetFiles(directoryPath, "*.xml");
- string fileContent = "";
-
-
-
-
-
- try
- {
- ExcelPackage ps = new ExcelPackage();
- ps.Workbook.Worksheets.Add("EMR-NDR");
- ExcelWorksheet ws = ps.Workbook.Worksheets[1];
- object missValue = System.Reflection.Missing.Value;
-
- Session["filename"] ="";
-
- ps.Workbook.Properties.Author = "Abraham Oaltubosun";
- ps.Workbook.Properties.Title = "EMR-NDR Data Extraction Analytics";
-
- ExcelRange ChartRange = ws.Cells["A1:D1"];
- ws.Cells[1, 1].Value = "Fil Names";
- ws.Cells[1, 2].Value = "HIV Encounter";
- ws.Cells[1, 3].Value = "CD4";
- ws.Cells[1, 4].Value = "Viral Load";
- ws.Cells[1, 5].Value = "ART Regimen";
-
- int t = 2;
- foreach (string file in filePaths)
- {
-
- Session["filename"] = file;
-
- fileContent = File.ReadAllText(file).Trim();
- if (fileContent.Length > 0)
- {
- XDocument xd = XDocument.Parse(fileContent);
- XmlDocument xxd = new XmlDocument();
- xxd.Load(file);
-
- int HIVEnCounter = 0;
-
- int RegimenCounter1 = 0;
- int LaboratoryCounter=0;
- int ViralLoadCounter = 0;
-
- string result = "";
-
- HIVEnCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/Encounters/HIVEncounter").Count;
-
- string result2 = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();
-
- if (!result2.Contains("Regimen") && !result2.Contains("Encounters"))
- {
- result =(string)xd.Element("Container").Element("IndividualReport").Element("Condition").Element("Regimen").Element("PrescribedRegimenTypeCode").Value;
- }
-
- if (!string.IsNullOrEmpty(result))
- {
- if (result == "ART")
- {
- #region
-
-
-
- #endregion
-
- RegimenCounter1 = xxd.SelectNodes("/Container/IndividualReport/Condition/Regimen/PrescribedRegimenTypeCode[. = \"ART\"]").Count;
- }
- }
-
- string result3 = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();
- if (result3.Contains("LaboratoryReport") && result3.Contains("LaboratoryResultedTest"))
- {
- result = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("LaboratoryReport").Element("LaboratoryOrderAndResult").Element("LaboratoryResultedTest").Element("CodeDescTxt").Value;
- }
-
- if (!string.IsNullOrEmpty(result))
- {
- if (result == "CD4")
- {
- LaboratoryCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"CD4\"]").Count;
-
- }
- }
-
- string resultVL = xd.Element("Container").Element("IndividualReport").Element("Condition").ToString();
-
- if (resultVL.Contains("LaboratoryReport") && resultVL.Contains("LaboratoryResultedTest"))
- {
- resultVL = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("LaboratoryReport").Element("LaboratoryOrderAndResult").Element("LaboratoryResultedTest").Element("CodeDescTxt").Value;
- }
- if (!string.IsNullOrEmpty(resultVL))
- {
-
-
- ViralLoadCounter = xxd.SelectNodes("/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"Viral Load\"]").Count;
-
-
- }
- #region
-
-
-
-
- #endregion
-
- ws.Cells[t, 1].Value = file;
- ws.Cells[t, 2].Value = HIVEnCounter;
- ws.Cells[t, 3].Value = LaboratoryCounter;
- ws.Cells[t, 4].Value = ViralLoadCounter;
- ws.Cells[t, 5].Value = RegimenCounter1;
-
- t = t + 1;
- }
- else
- {
-
- }
-
- }
-
-
- Byte[] bin = ps.GetAsByteArray();
- string files = directoryPath + "\\EMR-NDR.xlsx";
- File.WriteAllBytes(files, bin);
- Response.ClearContent();
- Response.Buffer = true;
- Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", files));
- Response.ContentType = "application/ms-excel";
- Response.WriteFile(files);
- Response.End();
- }
- catch (Exception ex) { webMessage.Show("Error in "+Session["filename"].ToString()+" :" + ex.Message); }
- }
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