2017-04-20 1 views
0

Ich habe bereits einen Code zum Deserialisieren einer XML-Datei mit Root-Element und nur elemnts geschrieben, das Problem, dass ich eine XML-Datei, die Root-Element enthält, deserialize, Elemente und Kindelement. Die Datei sieht wie folgt aus:Deserialize Xml-Datei mit Stammelement, Element und Kindelement C#

<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> 

Während meine Methode sieht wie folgt aus:

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; 
    } 

Die Frage ist, wie dieses Kind tun bekommen elemnt in Meine Liste (deserialisiert)

<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

Das ist eine Menge Arbeit für etwas, das mit dem fast automatisch dem eingebauten in Deserialisierung Routinen. Sehen Sie [diese Frage und ihre Antworten] (http://stackoverflow.com/q/364253/215552) für wie das funktioniert. –

+0

Ich habe vergessen zu erwähnen, dass ich auf asp.net Core arbeite und die obige Lösung hat nicht richtig funktioniert –

Antwort

0

Angenommen, Sie wissen, dass es nur an einem Ort sein, dann können Sie etwas tun:

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 viel! Es war sehr hilfreich –