Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 113.6k

HOW TO CHECK FOR SUB ELEMENT OF AN XML FILE

May 18 2018 2:26 AM
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:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. using System.IO;  
  10. using OfficeOpenXml;  
  11. using System.Xml;  
  12.   
  13. public partial class frm_analytics : System.Web.UI.Page  
  14. {  
  15.   
  16.     public Int32 XmlAnalytics(string mFileName, string mElements)  
  17.     {  
  18.         int cnt = 0;  
  19.         try  
  20.         {  
  21.             using (var reader = XmlReader.Create(mFileName))  
  22.             {  
  23.                 while (reader.Read())  
  24.                 {  
  25.                     if (reader.NodeType == XmlNodeType.Element && reader.Name == mElements)  
  26.                     {  
  27.                         cnt++;  
  28.                     }  
  29.   
  30.                 }  
  31.             }  
  32.   
  33.   
  34.   
  35.         }  
  36.         catch (Exception ex)  
  37.         {  
  38.             //webMessage.Show("Error Reading XML files");  
  39.         }  
  40.         return cnt;  
  41.   
  42.   
  43.     }  
  44.   
  45.     protected void Page_Load(object sender, EventArgs e)  
  46.     {  
  47.         //string directoryPath = "D:/TempDir/";  
  48.         string directoryPath = Server.MapPath(string.Format("~/{0}/""NDRExport"));  
  49.         string[] filePaths = Directory.GetFiles(directoryPath, "*.xml"); //, SearchOption.AllDirectories  
  50.         string fileContent = "";  
  51.   
  52.         /*========================================= 
  53.          * this section is to initialise the Excel header and 
  54.          * the workbook in turn the worksheet 
  55.          * */  
  56.         try  
  57.         {  
  58.             ExcelPackage ps = new ExcelPackage();  
  59.             ps.Workbook.Worksheets.Add("EMR-NDR");  
  60.             ExcelWorksheet ws = ps.Workbook.Worksheets[1];  
  61.             object missValue = System.Reflection.Missing.Value;  
  62.   
  63.             Session["filename"] = "";  
  64.   
  65.             ps.Workbook.Properties.Author = "Abraham Oaltubosun";  
  66.             ps.Workbook.Properties.Title = "EMR-NDR Data Extraction Analytics";  
  67.   
  68.             ExcelRange ChartRange = ws.Cells["A1:D1"];  
  69.             ws.Cells[1, 1].Value = "Fil Names";  
  70.             ws.Cells[1, 2].Value = "HIV Encounter";  
  71.             ws.Cells[1, 3].Value = "CD4";  
  72.             ws.Cells[1, 4].Value = "Viral Load";  
  73.             ws.Cells[1, 5].Value = "ART Regimen";  
  74.            // ws.Cells[1, 6].Value = "Prescribed Regimen";  
  75.   
  76.             int t = 2;  
  77.             foreach (string file in filePaths)  
  78.             {  
  79.                  
  80.   
  81.                 //int HIVEnCounter = xd.Descendants("HIVEncounter").Count();  
  82.                 int HIVEnCounter = XmlAnalytics(file, "HIVEncounter");  
  83.                 ////int RegimenCounter = 0;  
  84.                 int RegimenCounter1 = XmlAnalytics(file, "Regimen");  
  85.                 int LaboratoryCounter = XmlAnalytics(file, "Laboratory");  
  86.                 int ViralLoadCounter = XmlAnalytics(file, "Viral Load");  
  87.                // int PrescribedReg = XmlAnalytics(file, "PrescribedRegimen/code");  
  88.   
  89.                 ws.Cells[t, 1].Value = file;  
  90.                 ws.Cells[t, 2].Value = HIVEnCounter;  
  91.                 ws.Cells[t, 3].Value = LaboratoryCounter;  
  92.                 ws.Cells[t, 4].Value = ViralLoadCounter;  
  93.                 ws.Cells[t, 5].Value = RegimenCounter1;  
  94.                // ws.Cells[t, 5].Value = PrescribedReg;  
  95.                t++;  
  96.             }  
  97.             //============ Download Excel file =====================  
  98.             //Generate A File with Random name  
  99.             Byte[] bin = ps.GetAsByteArray();  
  100.             string files = directoryPath + "\\EMR-NDR.xlsx";  
  101.             File.WriteAllBytes(files, bin);  
  102.             Response.ClearContent();  
  103.             Response.Buffer = true;  
  104.             Response.AddHeader("Content-Disposition"string.Format("attachment; filename={0}", files));  
  105.             Response.ContentType = "application/ms-excel";  
  106.             Response.WriteFile(files);  
  107.             Response.End();  
  108.   
  109.             lblErr.Text = "Success....";  
  110.         }  
  111.         catch (Exception ex)  
  112.         {  
  113.            lblErr.Text="Error in " + Session["filename"].ToString() + " :" + ex.Message;  
  114.         }  
  115.     }  
  116. }  
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 
  1. <Regimen>  
  2.        <VisitID>7038</VisitID>  
  3.        <VisitDate>2013-09-05</VisitDate>  
  4.        <PrescribedRegimen>  
  5.          <Code>1e</Code>  
  6.          <CodeDescTxt>TDF/3TC/NVP</CodeDescTxt>  
  7.        </PrescribedRegimen>  
  8.        <PrescribedRegimenTypeCode>ART</PrescribedRegimenTypeCode>  
  9.        <PrescribedRegimenDuration>56</PrescribedRegimenDuration>  
  10.        <PrescribedRegimenDispensedDate>2013-09-05</PrescribedRegimenDispensedDate>  
  11.        <DateRegimenStarted>2011-02-10</DateRegimenStarted>  
  12.      </Regimen>  
  13.      <Regimen>  
  14.        <VisitID>7598</VisitID>  
  15.        <VisitDate>2013-10-31</VisitDate>  
  16.        <PrescribedRegimen>  
  17.          <Code>1e</Code>  
  18.          <CodeDescTxt>TDF/3TC/NVP</CodeDescTxt>  
  19.        </PrescribedRegimen>  
  20.        <PrescribedRegimenTypeCode>ART</PrescribedRegimenTypeCode>  
  21.        <PrescribedRegimenDuration>84</PrescribedRegimenDuration>  
  22.        <PrescribedRegimenDispensedDate>2013-10-31</PrescribedRegimenDispensedDate>  
  23.        <DateRegimenStarted>2011-02-10</DateRegimenStarted>  
  24.      </Regimen>  
I appreciate any help. 
Thank you all 

Answers (1)