1

Here is the method where I have the problem:

[HttpPost]
    public async Task<IActionResult> XmlPage(IFormFile xmlFile)
    { 
      var uploads = hostingEnvironment.WebRootPath;
        if (xmlFile.ContentType.Equals("application/xml") || xmlFile.ContentType.Equals("text/xml"))
        {
            try
            {
                using (var fileStream = new FileStream(Path.Combine(uploads, xmlFile.FileName), FileMode.Create))
                {
                    await xmlFile.CopyToAsync(fileStream);
                    XDocument xDoc = XDocument.Load(fileStream);
                    List<DmgRegister> dmgRegistterList = xDoc.Descendants("Claim").Select(dmgReg =>
                    new DmgRegister
                    {
                        Uwyear = dmgReg.Element("UWYear").Value,
                        ClaimNo = dmgReg.Element("ClaimNo").Value,
                        PolicyNo = dmgReg.Element("PolicyNo").Value,
                        PolicyName = dmgReg.Element("PolicyHolder").Value,
                        Address1 = dmgReg.Element("Address1").Value,
                        Address2 = dmgReg.Element("Addresstype").Value,
                        PostalLocation = dmgReg.Element("City").Value,
                        Country = dmgReg.Element("Country").Value,
                        PostalCode = dmgReg.Element("Postalzone").Value
                    }).ToList();
                    context.SaveXmlToDb(dmgRegistterList);

            }
            catch
            {
//Here where i come when i try to upload and parse the data
                ViewBag.Error = "Converting fail";
                return View("Export");
            }
        }
        else
        {
            ViewBag.Error = "Uploading fail";
            return View("Index");
        }
        return View();
    }

Im trying to upload an xml file and then parse into a SQL Table(Sql DataTable named DmgRegister), I'm a beginner with ASP.Net so exuse me if the code looks like an italian spaghetti.

       // SaveXmlToDb method in context 
          foreach (var item in dmgRegistterList)
            {
                DmgRegister.Add(item);
            }
        SaveChanges();
                }

Now the Model that I'm trying to bind data from XML and save it to DataTable

 [Serializable]
[XmlRoot("Claims")]
public class Claim
{
    [XmlElement("ClientName")]
    public string ClientName { get; set; }
    [XmlElement("UWYear")]
    public string Uwyear { get; set; }
    [XmlElement("AgreementNo")]
    public string AgreementNo { get; set; }
    [XmlElement("BusinessType")]
    public string BusinessType { get; set; }
    [XmlElement("PeriodStart")]
    public DateTime? PeriodStart { get; set; }
    [XmlElement("PeriodEnd")]
    public DateTime? PeriodEnd { get; set; }
    [XmlElement("PolicyNo")]
    public string PolicyNo { get; set; }
    [XmlElement("PolicyHolder")]
    public string PolicyName { get; set; }
    [XmlElement("DateOfLoss")]
    public DateTime? DateOfLoss { get; set; }
    [XmlElement("ClaimNo")]
    public string ClaimNo { get; set; }
    [XmlElement("ClaimantName")]
    public string ClaimantName { get; set; }
    [XmlElement("ClaimedInsured")]
    public string ClaimedInsured { get; set; }
    [XmlElement("ReportDate")]
    public DateTime? ReportDate { get; set; }
    [XmlElement("CountryOfRisk")]
    public string CountryOfRisk { get; set; }
    [XmlElement("CountryOfLoss")]
    public string CountryOfLoss { get; set; }
    [XmlElement("TypeOfLoss")]
    public string TypeOfLoss { get; set; }
    [XmlElement("InsuranceCoverage")]
    public string InsuranceCoverage { get; set; }
    [XmlElement("LineOfBuisnessNo")]
    public int? LineOfBuisnessNo { get; set; }
    [XmlElement("LineOfBuisnessName")]
    public string LineOfBuisnessName { get; set; }
    [XmlElement("ClassOfBuisnessNo")]
    public int? ClassOfBuisnessNo { get; set; }
    [XmlElement("ClassOfBuisnessName")]
    public string ClassOfBuisnessName { get; set; }
    [XmlElement("CaptiveShare")]
    public int? CaptiveShare { get; set; }
    [XmlElement("OriginalCurrency")]
    public string OriginalCurrency { get; set; }
    [XmlElement("PaymentCurrency")]
    public string PaidInCurrency { get; set; }
    [XmlElement("TotalIncurredCaptiveShare")]
    public decimal? TotalIncurredCaptiveShare { get; set; }
    [XmlElement("PaidThisPeriod")]
    public decimal? PaidThisPeriod { get; set; }
    [XmlElement("PeriodDate")]
    public DateTime? PeriodDate { get; set; }
    [XmlElement("PaymentDate")]
    public DateTime? PaymentDate { get; set; }
    [XmlElement("TotalPaidCaptiveShare")]
    public decimal? TotalPaidCaptiveShare { get; set; }
    [XmlElement("RemainingReserveCaptiveShare")]
    public decimal? RemainingReserveCaptiveShare { get; set; }
    [XmlElement("Deductible")]
    public string Deductible { get; set; }
    [XmlElement("Recovery")]
    public string Recovery { get; set; }
    [XmlElement("LocationAdress")]
    public string LocationAdress { get; set; }
    [XmlElement("Address1")]
    public string Address1 { get; set; }
    [XmlElement("Addresstype")]
    public string Address2 { get; set; }
    [XmlElement("Postalzone")]
    public string PostalCode { get; set; }
    [XmlElement("City")]
    public string PostalLocation { get; set; }
    [XmlElement("Country")]
    public string Country { get; set; }
    [XmlElement("GeograficalDiversification")]
    public string GeograficalDiversification { get; set; }
    [XmlElement("Cause")]
    public string Cause { get; set; }
    [XmlElement("Status")]
    public string Status { get; set; }
    [XmlElement("CloseDate")]
    public DateTime? CloseDate { get; set; }
    [XmlElement("DevelopmentYear")]
    public string DevelopmentYear { get; set; }
    [XmlElement("TotalIncurredInsurerShare")]
    public decimal? TotalIncurredInsurerShare { get; set; }
    [XmlElement("TotalPaidInsurerShare")]
    public decimal? TotalPaidInsurerShare { get; set; }
    [XmlElement("RemainingReserveInsurerShare")]
    public decimal? RemainingReserveInsurerShare { get; set; }

}
[Serializable()]
[XmlRoot("Claims")]
public class Claims
{
    [XmlArray("Claims")]
    [XmlArrayItem("Claim", typeof(Claim))]
    public Claim[] Claim { get; set; }
}

Here is the Xml example that im try upload

<ns0:Claims>
<Claim>
<ClaimNo>LL0000110262</ClaimNo>
<PolicyNo>LP0000004481</PolicyNo>
<PolicyHolder>NCC Rakennus Oy</PolicyHolder>
<AddressId>1</AddressId>
<Address1>Example Street 1</Address1>
<Addresstype>LocationOfLoss</Addresstype>
<City>Helsinki</City>
<Country>FI</Country>
<Postalzone>12345</Postalzone>
<UWYear>2015</UWYear>
<PeriodStart>2015-01-01</PeriodStart>
<PeriodEnd>2015-12-31</PeriodEnd>
<DateOfLoss>2015-07-15</DateOfLoss>
<ReportDate/>
<StatusAsPer>2015-12-31</StatusAsPer>
<Coverage>?</Coverage>
<TypeOfLoss>Leakage</TypeOfLoss>
<OriginalCurrency>EUR</OriginalCurrency>
<PaymentCurrency>EUR</PaymentCurrency>
<TotalReservesOrigCurr>0.00</TotalReservesOrigCurr>
<TotalPaymentOrigCurr>0.00</TotalPaymentOrigCurr>
<DeductibleOrigCurr>85000.00</DeductibleOrigCurr>
<TotalAmountOfClaimOrigCurr>3680.00</TotalAmountOfClaimOrigCurr>
<Status>Active</Status>
</Claim>
</ns0:Claims>

I try it even to convert from XML to JSON, but it jumps to catch

        public async Task<IActionResult> XmlPage(IFormFile xmlFile)
    {
        var uploads = hostingEnvironment.WebRootPath;
        var filePath = Path.Combine(uploads, xmlFile.FileName);
        if (xmlFile.ContentType.Equals("application/xml") || xmlFile.ContentType.Equals("text/xml"))
        {
            try
            {
                    using (var xReader = XmlReader.Create(new StringReader(filePath)))
                    {
                        // This line skips the XML declaration, eg "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" - you can skip this if you don't have declaration as in your case
                        xReader.MoveToContent();
                        // Gets the actual XMLElement, if any
                        xReader.Read();
                        // Convert the xReader to an XNode for the Json serializer
                        XNode node = XNode.ReadFrom(xReader);
                        // Json output
                        string jsonText = JsonConvert.SerializeXNode(node);



                }
            }
            catch
            {
                ViewBag.Error = "Converting fail";
                return View("Export");
            }
        }
        else
        {
            ViewBag.Error = "Uploading fail";
            return View();
        }
        return View("Index");
    }
5
  • Please include the issue you are experiencing.Is it failing to build or is it an issue with the results you are getting? AFAIK dotnet core doesn't have much in the way of Xml support yet. You can sidestep this issue by using Json instead. Commented Apr 20, 2017 at 9:50
  • I'm getting the xml file uploaded in wwwroot, but the point there it suppose to read from this file into a list fails and program run ahead to the Catch Commented Apr 20, 2017 at 10:39
  • It would be helpful to have the exception it's throwing. Update the try statement catch it: try { } catch(Exception e){ }. What is the error in e? Commented Apr 20, 2017 at 12:01
  • "Root element is missing" Commented Apr 20, 2017 at 12:10
  • And when i change--- XDocument xDoc = XDocument.Load(fileStream);---- to----- XDocument xDoc = XDocument.Load(filePath);--- then i get this error msg The process cannot access the file 'C:\Users\Housam\Desktop\LIA\CapinPhase1\CapinSolutions\CapinSolutions\wwwroot\Solvency2_Insurance_Schema_examples_If_20160208.xml' because it is being used by another process. Commented Apr 20, 2017 at 12:16

2 Answers 2

1

Thx a lot CountZero, really gratefull after my first question here, still had issues but now is everything working, The method in controller

       public async Task<IActionResult> XmlPage(IFormFile xmlFile)
    {
        var uploads = hostingEnvironment.WebRootPath;
        var filePath = Path.Combine(uploads, xmlFile.FileName).ToString();

        if (xmlFile.ContentType.Equals("application/xml") || xmlFile.ContentType.Equals("text/xml"))
        {
            try
            {
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await xmlFile.CopyToAsync(fileStream);
                    fileStream.Dispose();
                    XDocument xDoc = XDocument.Load(filePath);
                    List<DmgRegisterVM> dmgRegistterList = xDoc.Descendants("Claim").Select(dmgReg =>
                    new DmgRegisterVM
                    {
                        Uwyear = dmgReg.Element("UWYear").Value,
                        ClaimNo = dmgReg.Element("ClaimNo").Value,
                        PolicyNo = dmgReg.Element("PolicyNo").Value,
                        PolicyName = dmgReg.Element("PolicyHolder").Value,
                        Address1 = dmgReg.Element("Address1").Value,
                        Address2 = dmgReg.Element("Addresstype").Value,
                        PostalLocation = dmgReg.Element("City").Value,
                        Country = dmgReg.Element("Country").Value,
                        PostalCode = dmgReg.Element("Postalzone").Value
                    }).ToList();
                    context.SaveXmlToDb(dmgRegistterList);
                }
            }
            catch(Exception e)
            {
                    ViewBag.Error = "Converting fail";
            }
        }
        else
        {
            ViewBag.Error = "Uploading fail";
        }
        return View("Index");
    }

SaveXmlToDb method in context

        public void SaveXmlToDb(List<DmgRegisterVM> dmgRegList)
    {
        //from list to database
        foreach (var item in dmgRegList)
        {
            var model = Mapper.Map<DmgRegister>(item);
            DmgRegister.Add(model);
            SaveChanges();         
        }
    }

And finally the Claim class (where i have XML elements)

 [Serializable]
[XmlRoot("ns0:Claims")]
public class Claim
{
    [XmlElement("ClientName")]
    public string ClientName { get; set; }
    [XmlElement("UWYear")]
    public string Uwyear { get; set; }
    [XmlElement("AgreementNo")]
    public string AgreementNo { get; set; }
    [XmlElement("BusinessType")]
    public string BusinessType { get; set; }
    [XmlElement("PeriodStart")]
    public DateTime? PeriodStart { get; set; }
    [XmlElement("PeriodEnd")]
    public DateTime? PeriodEnd { get; set; }
    [XmlElement("PolicyNo")]
    public string PolicyNo { get; set; }
    [XmlElement("PolicyHolder")]
    public string PolicyName { get; set; }
    [XmlElement("DateOfLoss")]
    public DateTime? DateOfLoss { get; set; }
    [XmlElement("ClaimNo")]
    public string ClaimNo { get; set; }
    [XmlElement("ClaimantName")]
    public string ClaimantName { get; set; }
    [XmlElement("ClaimedInsured")]
    public string ClaimedInsured { get; set; }
    [XmlElement("ReportDate")]
    public DateTime? ReportDate { get; set; }
    [XmlElement("CountryOfRisk")]
    public string CountryOfRisk { get; set; }
    [XmlElement("CountryOfLoss")]
    public string CountryOfLoss { get; set; }
    [XmlElement("TypeOfLoss")]
    public string TypeOfLoss { get; set; }
    [XmlElement("InsuranceCoverage")]
    public string InsuranceCoverage { get; set; }
    [XmlElement("LineOfBuisnessNo")]
    public int? LineOfBuisnessNo { get; set; }
    [XmlElement("LineOfBuisnessName")]
    public string LineOfBuisnessName { get; set; }
    [XmlElement("ClassOfBuisnessNo")]
    public int? ClassOfBuisnessNo { get; set; }
    [XmlElement("ClassOfBuisnessName")]
    public string ClassOfBuisnessName { get; set; }
    [XmlElement("CaptiveShare")]
    public int? CaptiveShare { get; set; }
    [XmlElement("OriginalCurrency")]
    public string OriginalCurrency { get; set; }
    [XmlElement("PaymentCurrency")]
    public string PaidInCurrency { get; set; }
    [XmlElement("TotalIncurredCaptiveShare")]
    public decimal? TotalIncurredCaptiveShare { get; set; }
    [XmlElement("PaidThisPeriod")]
    public decimal? PaidThisPeriod { get; set; }
    [XmlElement("PeriodDate")]
    public DateTime? PeriodDate { get; set; }
    [XmlElement("PaymentDate")]
    public DateTime? PaymentDate { get; set; }
    [XmlElement("TotalPaidCaptiveShare")]
    public decimal? TotalPaidCaptiveShare { get; set; }
    [XmlElement("RemainingReserveCaptiveShare")]
    public decimal? RemainingReserveCaptiveShare { get; set; }
    [XmlElement("Deductible")]
    public string Deductible { get; set; }
    [XmlElement("Recovery")]
    public string Recovery { get; set; }
    [XmlElement("LocationAdress")]
    public string LocationAdress { get; set; }
    [XmlElement("Address1")]
    public string Address1 { get; set; }
    [XmlElement("Addresstype")]
    public string Address2 { get; set; }
    [XmlElement("Postalzone")]
    public string PostalCode { get; set; }
    [XmlElement("City")]
    public string PostalLocation { get; set; }
    [XmlElement("Country")]
    public string Country { get; set; }
    [XmlElement("GeograficalDiversification")]
    public string GeograficalDiversification { get; set; }
    [XmlElement("Cause")]
    public string Cause { get; set; }
    [XmlElement("Status")]
    public string Status { get; set; }
    [XmlElement("CloseDate")]
    public DateTime? CloseDate { get; set; }
    [XmlElement("DevelopmentYear")]
    public string DevelopmentYear { get; set; }
    [XmlElement("TotalIncurredInsurerShare")]
    public decimal? TotalIncurredInsurerShare { get; set; }
    [XmlElement("TotalPaidInsurerShare")]
    public decimal? TotalPaidInsurerShare { get; set; }
    [XmlElement("RemainingReserveInsurerShare")]
    public decimal? RemainingReserveInsurerShare { get; set; }

}
//[ModelMetadataType(typeof(DmgRegisterMetaData))]
[Serializable()]
[XmlRoot("ns0:Claims")]
public class ClaimsCollection
{
    [XmlArray("Claims")]
    [XmlArrayItem("Claim", typeof(Claim))]
    public Claim[] Claim { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help :)
0

Given you are getting a "Root element is missing" this looks like an issue with your mapping.

[XmlRoot("Claims")] will not map onto <ns0:Claims>.

Try updating your XML so the roots element is set to <Claims></Claims> rather than

<ns0:Claims></ns0:Claims>.

If this fixes it then you can either change your XML or update your attribute in the class so it looks like this.

[XmlRoot("ns0:Claims")].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.