TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
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;
}
}
}
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
Attachment:
CIHPINITIALCT00724NDR.zip
Reply
Answers (
1
)
run time set printer name to print in crystal report
How to read xml from string variable with particular tag