Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
Tracking Number of Element (Nods or Sub-Nods) in an XML file
Abraham Olatubosun
Aug 24 2017 3:32 AM
213
Reply
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 = "D:/TempDir/";
string
directoryPath = Server.MapPath(
string
.Format(
"~/{0}/"
,
"XML"
));
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"
;
int
t = 2;
foreach
(
string
file
in
filePaths)
{
// Log Message read started
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 = xd.Descendants("HIVEncounter").Count();
int
HIVEnCounter = 0;
////int RegimenCounter = 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
//RegimenCounter = xd.Element("Container").Element("IndividualReport").Element("Condition").Element("Regimen").Element("PrescribedRegimenTypeCode").Attributes("ART").Count();
//RegimenCounter = xd.Descendants("Regimen").Count();
//RegimenCounter = xxd.SelectNodes("").Count;
#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;
//LaboratoryCounter = xd.Descendants("LaboratoryReport").Count();
}
}
string
resultVL = xd.Element(
"Container"
).Element(
"IndividualReport"
).Element(
"Condition"
).ToString();
//resultVL = xxd.SelectNodes
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))
{
//if (resultVL == "Viral Load")
//{
ViralLoadCounter = xxd.SelectNodes(
"/Container/IndividualReport/Condition/LaboratoryReport/LaboratoryOrderAndResult/LaboratoryResultedTest/CodeDescTxt[. =\"Viral Load\"]"
).Count;
//ViralLoadCounter = xd.Descendants("LaboratoryReport").Count();
//}
}
#region
//int CD4Counts = xd.Descendants("CD4").Count();
//int LaboratoryCounter = xd.Descendants("LaboratoryOrderAndResult").Count();
//var border = ws.Cells.Style.Border;
//border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin; //ExcelBorderStyle.Thin;
#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
{
// Log error File is blank
}
}
//============ 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();
}
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
Attachment:
GHOKIGWE_APR_AUG_.zip
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
How deploy system vb 2015 with sql database server .
Bind Data JSTree