这里就是我有问题的方法:从XML文件读入现有的dataTable C#ASP.NET核心
[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();
}
我试着上传一个XML文件,然后解析到一个SQL表(SQL数据表命名DmgRegister),我是一个ASP.Net的初学者,如果代码看起来像意大利式细面条,那么我就会滥用我。
// SaveXmlToDb method in context
foreach (var item in dmgRegistterList)
{
DmgRegister.Add(item);
}
SaveChanges();
}
现在,我想从XML绑定的数据并将其保存到数据表
[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; }
}
这里的模型是XML例子,即时通讯试图上传
<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>
我尝试它甚至可以将XML从XML转换为JSON,但它可以跳跃式捕捉到
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");
}
请注明你是它未能建立experiencing.Is或者是与你所得到的结果的问题的问题? AFAIK dotnet核心还没有太多的Xml支持。您可以通过使用Json来避开这个问题。 – CountZero
我得到的是在wwwroot上传的xml文件,但是它假设从这个文件读入列表失败,并且程序运行到Catch –
这将有助于抛出异常。更新try语句捕获它:尝试{} catch(Exception e){}。 e中的错误是什么? – CountZero