2017-04-20 156 views
0

我已经写了一个代码来反序列化一个xml文件,其中只包含根元素和元素,问题是我需要它反序列化包含根元素的XML文件,元素和CHILD元素。 该文件是这样的:反序列化带有根元素,元素和子元素c的xml文件#

<Claim> 
<ClaimNo>LL0000110262</ClaimNo> 
<PolicyNo>LP0000004481</PolicyNo> 
<PolicyHolder>EST Boras AB</PolicyHolder> 
<Locations> 
    <Location> 
    <AddressId>1</AddressId> 
    <Address1>Example Street 1</Address1> 
    <Addresstype>LocationOfLoss</Addresstype> 
    <City>Helsinki</City> 
    <Country>FI</Country> 
    <Postalzone>12345</Postalzone> 
    </Location> 
</Locations> 
<UWYear>2015</UWYear> 
<PeriodStart>2015-01-01</PeriodStart> 
<PeriodEnd>2015-12-31</PeriodEnd> 
<DateOfLoss>2015-07-15</DateOfLoss> 
<ReportDate></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> 

虽然我的方法是这样的:

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 = GetDmgFromXml(xDoc); 
        context.SaveXmlToDb(dmgRegistterList); 
       } 
      } 
      // returning at httpGet with a temp message that says att uploadin is failed 
      catch 
      { 
       ViewBag.Error = "Converting fail"; 
      } 
     } 
     // returning at httpGet with a temp message that says att uploadin is failed 
     else 
     { 
      ViewBag.Error = "Uploading fail"; 
     } 
     return View("Index"); 
    } 

    private List<DmgRegisterVM> GetDmgFromXml(XDocument xDoc) 
    { 
     var list = 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(); 
     return list; 
    } 

的问题是如何做到让这孩子elemnt成我的清单(反序列化)

<Locations> 
<Location> 
<AddressId>1</AddressId> 
<Address1>Example Street 1</Address1> 
<Addresstype>LocationOfLoss</Addresstype> 
<City>Helsinki</City> 
<Country>FI</Country> 
<Postalzone>12345</Postalzone> 
</Location> 
</Locations> 
+0

这是什么东西了很多工作,这是几乎自动的内置反序列化例程。请参阅[这个问题及其答案](http://stackoverflow.com/q/364253/215552)了解如何工作。 –

+0

我可能忘了提及我正在研究asp.net核心,上面的解决方案没有正常工作 –

回答

0

假设你知道只会有一个位置,那么你可以做这样的事情:

var viewModels = 
    from claim in doc.Descendants("Claim") 
    from location in claim.Descendants("Location") 
    select new DmgRegisterVM 
    { 
     Uwyear = (string) claim.Element("UWYear"), 
     ClaimNo = (string) claim.Element("ClaimNo"), 
     PolicyNo = (string) claim.Element("PolicyNo"), 
     PolicyName = (string) claim.Element("PolicyHolder"), 
     Address1 = (string) location.Element("Address1"), 
     Address2 = (string) location.Element("Addresstype"), 
     PostalLocation = (string) location.Element("City"), 
     Country = (string) location.Element("Country"), 
     PostalCode = (string) location.Element("Postalzone") 
    }; 
+0

Thx很多!这非常有帮助 –