以下是我遇到问题的方法:
public static void main(String[] args){
List<String> parole = Arrays.asList("Lambda ", "expressions ", "are ", "cool ");
Predicate <String> findAre= s->"are ".equals(s);
parole.removeIf(findAre);
parole.forEach( System.out::println);
}
我试图上传一个xml文件,然后解析成一个SQL表(Sql DataTable,名为DmgRegister),我是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绑定数据并将其保存到DataTable的模型
// 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转换为JSON,但它会跳转到捕捉
<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>
答案 0 :(得分:0)
鉴于您正在获取“Root元素缺失”,这看起来像是您的映射问题。
[XmlRoot("Claims")]
不会映射到<ns0:Claims>
。
尝试更新XML,以便将roots元素设置为<Claims></Claims>
而不是
<ns0:Claims></ns0:Claims>
。
如果这样可以解决问题,那么您可以更改XML或更新类中的属性,使其看起来像这样。
[XmlRoot("ns0:Claims")]
。
答案 1 :(得分:0)
很多CountZero,在我第一个问题之后真的很感激,仍然有问题,但现在一切正常, 控制器中的方法
<div>
Hello
<div class="search">
<div class="tip">
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus
enim ac dui. Donec non enim in turpis pulvinar facilisis
</div>
</div>
</div>
<div>
Hello AGain
<div class="search">
<div class="tip ">
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris
placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus
enim ac dui. Donec non enim in turpis pulvinar facilisis
</div>
</div>
</div>
上下文中的SaveXmlToDb方法
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");
}
最后是Claim类(我有XML元素)
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();
}
}