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)
- {
-
- }
- return cnt;
-
-
- }
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- string directoryPath = Server.MapPath(string.Format("~/{0}/", "NDRExport"));
- 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)
- {
-
-
-
- int HIVEnCounter = XmlAnalytics(file, "HIVEncounter");
-
- int RegimenCounter1 = XmlAnalytics(file, "Regimen");
- int LaboratoryCounter = XmlAnalytics(file, "Laboratory");
- int ViralLoadCounter = XmlAnalytics(file, "Viral Load");
-
-
- 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++;
- }
-
-
- 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;
- }
- }
- }
the code above is working very well, i want to in line 87 to test if it can find
PrescribedRegimen/code which under the main element
<Regimen>bellow is the portion of the xml file
- <Regimen>
- <VisitID>7038</VisitID>
- <VisitDate>2013-09-05</VisitDate>
- <PrescribedRegimen>
- <Code>1e</Code>
- <CodeDescTxt>TDF/3TC/NVP</CodeDescTxt>
- </PrescribedRegimen>
- <PrescribedRegimenTypeCode>ART</PrescribedRegimenTypeCode>
- <PrescribedRegimenDuration>56</PrescribedRegimenDuration>
- <PrescribedRegimenDispensedDate>2013-09-05</PrescribedRegimenDispensedDate>
- <DateRegimenStarted>2011-02-10</DateRegimenStarted>
- </Regimen>
- <Regimen>
- <VisitID>7598</VisitID>
- <VisitDate>2013-10-31</VisitDate>
- <PrescribedRegimen>
- <Code>1e</Code>
- <CodeDescTxt>TDF/3TC/NVP</CodeDescTxt>
- </PrescribedRegimen>
- <PrescribedRegimenTypeCode>ART</PrescribedRegimenTypeCode>
- <PrescribedRegimenDuration>84</PrescribedRegimenDuration>
- <PrescribedRegimenDispensedDate>2013-10-31</PrescribedRegimenDispensedDate>
- <DateRegimenStarted>2011-02-10</DateRegimenStarted>
- </Regimen>
I appreciate any help.
Thank you all