Dear Code Masters,
I believe this quetion meet you in good health, i am writting a module that will read an xml file and test if a sub element contain the a value such as Date.
This is what i have tried:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- using System.IO;
- using OfficeOpenXml;
- using System.Xml;
- public partial class frm_analytics : System.Web.UI.Page
- {
- public Int32 XmlAnalytics(string mFileName, string mElements)
- {
- int cnt = 0;
- try
- {
- using (var reader = XmlReader.Create(mFileName))
- {
- while (reader.Read())
- {
- if (reader.NodeType == XmlNodeType.Element && reader.Name == mElements)
- {
- cnt++;
- }
- }
- }
- }
- catch (Exception ex)
- {
- //webMessage.Show("Error Reading XML files");
- }
- return cnt;
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- //string directoryPath = "D:/TempDir/";
- string directoryPath = Server.MapPath(string.Format("~/{0}/", "NDRExport"));
- string[] filePaths = Directory.GetFiles(directoryPath, "*.xml"); //, SearchOption.AllDirectories
- string fileContent = "";
- /*=========================================
- * this section is to initialise the Excel header and
- * the workbook in turn the worksheet
- * */
- 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";
- // ws.Cells[1, 6].Value = "Prescribed Regimen";
- int t = 2;
- foreach (string file in filePaths)
- {
- //int HIVEnCounter = xd.Descendants("HIVEncounter").Count();
- int HIVEnCounter = XmlAnalytics(file, "HIVEncounter");
- ////int RegimenCounter = 0;
- int RegimenCounter1 = XmlAnalytics(file, "Regimen");
- int LaboratoryCounter = XmlAnalytics(file, "Laboratory");
- int ViralLoadCounter = XmlAnalytics(file, "Viral Load");
- // int PrescribedReg = XmlAnalytics(file, "PrescribedRegimen/code");
- 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;
- // ws.Cells[t, 5].Value = PrescribedReg;
- t++;
- }
- //============ Download Excel file =====================
- //Generate A File with Random name
- 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();
- lblErr.Text = "Success....";
- }
- catch (Exception ex)
- {
- lblErr.Text="Error in " + Session["filename"].ToString() + " :" + ex.Message;
- }
- }
- }
bellow is the portion of the xml file
- <Regimen>
- <VisitID>7038VisitID>
- <VisitDate>2013-09-05VisitDate>
- <PrescribedRegimen>
- <Code>1eCode>
- <CodeDescTxt>TDF/3TC/NVPCodeDescTxt>
- PrescribedRegimen>
- <PrescribedRegimenTypeCode>ARTPrescribedRegimenTypeCode>
- <PrescribedRegimenDuration>56PrescribedRegimenDuration>
- <PrescribedRegimenDispensedDate>2013-09-05PrescribedRegimenDispensedDate>
- <DateRegimenStarted>2011-02-10DateRegimenStarted>
- Regimen>
- <Regimen>
- <VisitID>7598VisitID>
- <VisitDate>2013-10-31VisitDate>
- <PrescribedRegimen>
- <Code>1eCode>
- <CodeDescTxt>TDF/3TC/NVPCodeDescTxt>
- PrescribedRegimen>
- <PrescribedRegimenTypeCode>ARTPrescribedRegimenTypeCode>
- <PrescribedRegimenDuration>84PrescribedRegimenDuration>
- <PrescribedRegimenDispensedDate>2013-10-31PrescribedRegimenDispensedDate>
- <DateRegimenStarted>2011-02-10DateRegimenStarted>
- Regimen>
Thank you all

Abraham OlatubosunPosted May 27, 2018, 1:01 AM