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

时间:2017-04-20 21:01:18

标签: c# xml xml-deserialization

  

我已经编写了一个代码来反序列化一个只包含root元素和elemnts的xml文件,我需要它来反序列化包含根元素,元素和CHILD元素的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;
    }
  

问题是如何让这个孩子进入我的列表(反序列化)

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

1 个答案:

答案 0 :(得分: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")
    };