Saima
New member
- Joined
- Nov 10, 2023
- Messages
- 1
- Programming Experience
- Beginner
Controller:
using System;
using System.Collections.Generic; // Add this line for List<T>
using System.IO;
using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using YSU.Models;
using System.Globalization;
public class XmlController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ILogger<XmlController> _logger;
public XmlController(IWebHostEnvironment webHostEnvironment, ILogger<XmlController> logger)
{
_webHostEnvironment = webHostEnvironment;
_logger = logger;
}
//Get Index
public IActionResult ReadXmlFiles()
{
string folderPath = Path.Combine(_webHostEnvironment.WebRootPath, "App_Data", "XML");
List<XmlViewModel> viewModels = new List<XmlViewModel>();
try
{
string[] xmlFiles = Directory.GetFiles(folderPath, "*.xml");
foreach (string filePath in xmlFiles)
{
XmlViewModel viewModel = ReadXmlFile(filePath);
if (viewModel != null)
{
viewModel.FileName = Path.GetFileName(filePath);
viewModels.Add(viewModel);
}
}
}
catch (Exception ex)
{
_logger.LogError($"Error while processing XML files: {ex.Message}");
return View("ErrorView"); // Ensure that "ErrorView.cshtml" exists in the appropriate location
}
return View(viewModels); // Pass the list of view models to the view
}
//Method for Serialize and Deserialize
public XmlViewModel ReadXmlFile(string filePath)
{
XmlViewModel viewModel = new XmlViewModel();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlViewModel));
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
viewModel = (XmlViewModel)serializer.Deserialize(fileStream);
}
// Log the awards count for debugging
_logger.LogInformation($"Awards count after deserialization: {viewModel.Awards?.Count}");
// Handle null Awards property
if (viewModel.Awards == null)
{
viewModel.Awards = new List<Award>();
}
// Log the awards count after handling null
_logger.LogInformation($"Awards count after handling null: {viewModel.Awards.Count}");
// Adjust date format
foreach (var award in viewModel.Awards)
{
// Your date parsing logic...
}
}
catch (Exception ex)
{
_logger.LogError($"Error during XML deserialization: {ex.Message}");
}
return viewModel;
}
}
Award:
using System;
using System.Xml.Serialization;
namespace YSU.Models
{
[XmlRoot("rootTag")]
public class Award
{
// Constructor to initialize the list
public Award()
{
Investigator = new Investigator();
AwardTitle = new AwardTitle();
AwardInstrument = new AwardInstrument();
}
[XmlElement(ElementName = "AwardTitle")]
public AwardTitle? AwardTitle { get; set; }
[XmlElement(ElementName = "AGENCY")]
public string? AGENCY { get; set; }
[XmlElement(ElementName = "AwardEffectiveDate")]
public DateTime AwardEffectiveDate { get; set; }
[XmlElement(ElementName = "AwardExpirationDate")]
public DateTime AwardExpirationDate { get; set; }
[XmlElement(ElementName = "AwardTotalIntnAmount")]
public decimal AwardTotalIntnAmount { get; set; }
[XmlElement(ElementName = "AwardAmount")]
public int AwardAmount { get; set; }
[XmlElement(ElementName = "AwardInstrument")]
public AwardInstrument? AwardInstrument { get; set; }
[XmlElement(ElementName = "Organization")]
public Organization? Organization { get; set; }
[XmlElement(ElementName = "ProgramOfficer")]
public ProgramOfficer? ProgramOfficer { get; set; }
[XmlElement(ElementName = "AbstractNarration")]
public string? AbstractNarration { get; set; }
[XmlElement(ElementName = "MinAmdLetterDate")]
public DateTime? MinAmdLetterDate { get; set; }
[XmlElement(ElementName = "MaxAmdLetterDate")]
public DateTime MaxAmdLetterDate { get; set; }
[XmlElement(ElementName = "ARRAAmount")]
public string? ARRAAmount { get; set; }
[XmlElement(ElementName = "TRAN_TYPE")]
public string? TRAN_TYPE { get; set; }
[XmlElement(ElementName = "CFDA_NUM")]
public string? CFDA_NUM { get; set; }
[XmlElement(ElementName = "NSF_PAR_USE_FLAG")]
public int NSF_PAR_USE_FLAG { get; set; }
[XmlElement(ElementName = "FUND_AGCY_CODE")]
public string FUND_AGCY_CODE { get; set; }
[XmlElement(ElementName = "AWDG_AGCY_CODE")]
public string AWDG_AGCY_CODE { get; set; }
[XmlElement(ElementName = "AwardID")]
public int AwardID { get; set; }
[XmlElement(ElementName = "Investigator")]
public Investigator Investigator { get; set; }
[XmlElement(ElementName = "Institution")]
public XmlViewModel.Institution? Institution { get; set; }
[XmlElement(ElementName = "Performance_Institution")]
public XmlViewModel.Performance_Institution PerformanceInstitution { get; set; }
[XmlElement(ElementName = "ProgramElement")]
public XmlViewModel.ProgramElement ProgramElement { get; set; }
[XmlElement(ElementName = "ProgramReference")]
public XmlViewModel.ProgramReference ProgramReference { get; set; }
[XmlElement(ElementName = "Appropriation")]
public XmlViewModel.Appropriation Appropriation { get; set; }
[XmlElement(ElementName = "Fund")]
public XmlViewModel.Fund? Fund { get; set; }
[XmlElement(ElementName = "FUND_OBLG")]
public string? FUND_OBLG { get; set; }
public string? FileName { get; internal set; }
}
public class AwardTitle
{
[XmlText]
public string? Value { get; set; }
}
public class AwardInstrument
{
public string? Value { get; set; }
}
public class Organization
{
public string? Code { get; set; }
public Directorate? Directorate { get; set; }
public Division? Division { get; set; }
}
public class Directorate
{
public string? Abbreviation { get; set; }
public string? LongName { get; set; }
}
public class Division
{
public string Abbreviation { get; set; }
public string LongName { get; set; }
}
public class ProgramOfficer
{
public string SignBlockName { get; set; }
public string PO_EMAI { get; set; }
public string PO_PHON { get; set; }
}
public class Investigator
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PI_FULL_NAME { get; set; }
public EmailAddress EmailAddress { get; set; }
public string NSF_ID { get; set; }
public DateTime StartDate { get; set; }
public string RoleCode { get; set; }
}
public class EmailAddress
{
[XmlText]
public string Value { get; set; }
}
}
ReadXmlFiles.chtml:
@model List<YSU.Models.XmlViewModel>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XML File Reader</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- Optional: jQuery -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<!-- Bootstrap JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<div class="container mt-4">
<h1 class="mb-4">XML Files Data</h1>
@if (Model != null && Model.Count > 0)
{
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Award Title</th>
<th>AGENCY</th>
<th>Award Effective Date</th>
<th>Award Expiration Date</th>
<th>Award Total Intn Amount</th>
<th>Award Amount</th>
<th>Award Instrument</th>
<th>Organization Code</th>
<th>Program Officer SignBlockName</th>
<th>Investigator FirstName</th>
<th>Investigator LastName</th>
<th>Institution Name</th>
</tr>
</thead>
<tbody>
@foreach (var viewModel in Model)
{
@if (viewModel.Awards != null && viewModel.Awards.Count > 0)
{
var award = viewModel.Awards[0]; // Access the first award
<tr>
<td>@award?.AwardTitle?.Value</td>
<td>@award?.AGENCY</td>
<td>@award?.AwardEffectiveDate.ToString("yyyy-MM-dd")</td>
<td>@award?.AwardExpirationDate.ToString("yyyy-MM-dd")</td>
<td>@award?.AwardTotalIntnAmount</td>
<td>@award?.AwardAmount</td>
<td>@award?.AwardInstrument?.Value</td>
<td>@award?.Organization?.Code</td>
<td>@award?.ProgramOfficer?.SignBlockName</td>
<td>@award?.Investigator?.FirstName</td>
<td>@award?.Investigator?.LastName</td>
<td>@award?.Institution?.Name</td>
</tr>
}
else
{
<tr>
<td colspan="12">No XML data available for @viewModel.FileName</td>
</tr>
}
}
</tbody>
</table>
</div>
}
else
{
<p>No XML data available.</p>
}
</div>
</body>
</html>
XmlViewModel:
using System;
using System.Xml.Serialization;
using System.IO;
namespace YSU.Models
{
[XmlRoot("rootTag")]
public class XmlViewModel
{
private List<Award> awardsList = new List<Award>();
[XmlArray("rootTag")]
[XmlArrayItem("Award")]
public List<Award> Awards
{
get => awardsList;
set => awardsList = value ?? new List<Award>();
}
public string FileName { get; internal set; }
public class ProgramElement
{
public string? Code { get; set; }
public string? Text { get; set; }
}
public class ProgramReference
{
public string? Code { get; set; }
public string?Text { get; set; }
}
public class Appropriation
{
public string? Code { get; set; }
public string? Name { get; set; }
public string? APP_SYMB_ID { get; set; }
}
public class Fund
{
public string? Code { get; set; }
[XmlText]
public string? Name { get; set; }
public string? FUND_SYMB_ID { get; set; }
}
public class Institution
{
public string Name { get; set; }
public string CityName { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public string StreetAddress { get; set; }
public string StreetAddress2 { get; set; }
public string CountryName { get; set; }
public string StateName { get; set; }
public string StateCode { get; set; }
public string CONGRESSDISTRICT { get; set; }
public string CONGRESS_DISTRICT_ORG { get; set; }
public string ORG_UEI_NUM { get; set; }
public string ORG_LGL_BUS_NAME { get; set; }
public string ORG_PRNT_UEI_NUM { get; set; }
}
public class Performance_Institution
{
public string Name { get; set; }
public string CityName { get; set; }
public string StateCode { get; set; }
public string ZipCode { get; set; }
public string StreetAddress { get; set; }
public string CountryCode { get; set; }
public string CountryName { get; set; }
public string StateName { get; set; }
public int CountryFlag { get; set; }
public string CONGRESSDISTRICT { get; set; }
public string CONGRESS_DISTRICT_PERF { get; set; }
}
}
}