Linq to XML完整的Child Element XL结构

我试图不使用读取xml的旧方法,并尝试对xml进行linq,但是我很难完成以下任务:

响应XML

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
      <PolicyResponse>
        <serviceResponse>
          <responseCode>0</responseCode>
          <responseDescription>Success</responseDescription>
          <responseDetail>Success</responseDetail>
          <paging>
            <pageNum>1</pageNum>
            <pageSize>3</pageSize>
            <totalRecords>3</totalRecords>
          </paging>
        </serviceResponse>
        <detailsResponseList>
          <detailResponse>
            <policy>
              <policySummaryInfo>
                <PolicyNumber>1199128</PolicyNumber>
                <PremiumChangeAmt>...</PremiumChangeAmt>
                <WrittenAmt>...</WrittenAmt>
                <PolicyStatusDesc>Expired</PolicyStatusDesc>
                <BillingInfo>
                  <InsuredOrPrincipal>...</InsuredOrPrincipal>
                </BillingInfo>
              </policySummaryInfo>
            </policy>
          </detailResponse>
          <detailResponse>
            <policy>
              <policySummaryInfo>
                <PolicyNumber>1199128</PolicyNumber>
                <PremiumChangeAmt>...</PremiumChangeAmt>
                <WrittenAmt>...</WrittenAmt>
                <PolicyStatusDesc>active</PolicyStatusDesc>
                <BillingInfo>
                  <InsuredOrPrincipal>...</InsuredOrPrincipal>
                </BillingInfo>
              </policySummaryInfo>
            </policy>
          </detailResponse>
        </detailsResponseList>
      </PolicyResponse>
    </out2:getPolicySummaryResponse>
  </soapenv:Body>
</soapenv:Envelope>

我正在用C#进行

XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(xmlResponse);

IEnumerable<XElement> items =
  from el in xdoc.Descendants("detailResponse")
   select el;

使用此代码,我可以读取bot detailReponse元素,但我要保留的是<PolicyStatusDesc></PolicyStatusDesc>等于active的XML detailResponse结构。那可能吗。我设法根据元素名称来收集特定的数据,但是如何使用Linq to XML保持整个XML子元素结构(detailResponse元素及其子元素)。

谢谢

bangegua 回答:Linq to XML完整的Child Element XL结构

您可以通过使用<PolicyStatusDesc>过滤Active个元素,然后选择那些具有值<detailResponse>的元素来获得el.Descendants("PolicyStatusDesc")具有值Active的元素。

使用查询语法:

var items = from el in xdoc.Descendants("detailResponse")
            where el.Descendants("PolicyStatusDesc").Any(p => p.Value == "Active")
            select el;

或使用方法语法:

 var items = xdoc.Descendants("detailResponse")
                 .Where(el => el.Descendants("PolicyStatusDesc")
                     .Any(p => p.Value == "Active"));
,
  

在您的XML示例中,无效   我已删除的</out2:getPolicySummaryResponse>标签

这是您可能用于结果的代码

 IEnumerable<XElement> items =
            from el in xdoc.Descendants("detailResponse")
            where  el.Element("policy")?.Element("policySummaryInfo")?.Element("PolicyStatusDesc")?.Value == "Active"
            select el;
,

我经常使用下面的代码,其结果比使用序列化更平坦:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement xPolicyResponse = doc.Descendants("PolicyResponse").FirstOrDefault();

            PolicyResponse policyResponse = new PolicyResponse(xPolicyResponse); 

        }
    }
    public class PolicyResponse
    {
        public ServiceResponse serviceResponse { get; set; }
        public List<DetailResponse> detailResponse { get; set; }

        public PolicyResponse() {}
        public PolicyResponse(XElement element)
        {
            XElement xServiceResponse = element.Element("serviceResponse");
            List<XElement> xdetailResponseList = element.Descendants("detailResponse").ToList();
            serviceResponse = new ServiceResponse(xServiceResponse);
            detailResponse = xdetailResponseList.Select(x => new DetailResponse(x)).ToList();

        }
    }
    public class ServiceResponse
    {
        public int responseCode { get; set; }
        public string responseDescription { get; set; }
        public string responseDetail { get; set; }
        public int pageNum { get; set; }
        public int pageSize { get; set; }
        public int totalRecords { get; set; }

        public ServiceResponse() { }
        public ServiceResponse(XElement element)
        {
            responseCode = (int)element.Element("responseCode");
            responseDescription = (string)element.Element("responseDescription");
            responseDetail = (string)element.Element("responseDetail");
            pageNum = (int)element.Descendants("pageNum").FirstOrDefault();
            pageSize = (int)element.Descendants("pageSize").FirstOrDefault();
            totalRecords = (int)element.Descendants("totalRecords").FirstOrDefault();
        }
    }
    public class DetailResponse
    {
        public string policyNumber { get; set; }
        public string premiumChangeAmt { get; set; }
        public string writtenAmt { get; set; }
        public string policyStatusDesc { get; set; }
        public string insuredOrPrincipal { get; set; }                

        public DetailResponse() { }
        public DetailResponse(XElement element)
        {
            XElement xPolicySummaryInfo = element.Descendants("policySummaryInfo").FirstOrDefault();

            policyNumber = (string)xPolicySummaryInfo.Element("PolicyNumber");
            premiumChangeAmt = (string)xPolicySummaryInfo.Element("PremiumChangeAmt");
            writtenAmt = (string)xPolicySummaryInfo.Element("WrittenAmt");
            policyStatusDesc = (string)xPolicySummaryInfo.Element("PolicyStatusDesc");
            insuredOrPrincipal = (string)xPolicySummaryInfo.Descendants("InsuredOrPrincipal").FirstOrDefault();
        }
    }
}
本文链接:https://www.f2er.com/3148302.html

大家都在问