简体   繁体   English

用根元素,elemnt和子元素c#反序列化Xml文件

[英]Deserialize Xml file with root element, elemnts and child element c#

I already wrote a code to deserialize an xml file with root element and elemnts only, the problem that i need it to deserialize a XML file that contains root element, elements and CHILD element. 我已经编写了一个代码,仅使用根元素和elemnts反序列化xml文件,这个问题是我需要它反序列化包含根元素,元素和CHILD元素的XML文件。 The file looks like this: 该文件如下所示:

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

While my method looks like this: 虽然我的方法如下所示:

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

The question is how do get this child elemnt into my list (deserialized) 问题是如何让这个孩子成为我的孩子(反序列化)

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

Assuming you know there will only be one location, then you can do something like this: 假设您知道只有一个位置,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM