Linq到XML查询问题

时间:2011-07-08 16:43:22

标签: linq-to-xml

这是我的XML:

<?xml version="1.0" ?> 
<AddressValidateResponse>
  <Address ID="0">
    <FirmName>FIRM NAME, INC</FirmName> 
    <Address2>123 MAIN ST</Address2> 
    <City>SOME PLACE</City> 
    <State>CA</State> 
    <Zip5>90028</Zip5> 
    <Zip4>1467</Zip4> 
  </Address>
  <Address ID="1">
    <Error>
      <Number>-2147219401</Number> 
      <Source>SOURCE INFO HERE</Source> 
      <Description>Address Not Found.</Description> 
      <HelpFile /> 
      <HelpContext>1000440</HelpContext> 
    </Error>
  </Address>
  <Address ID="2">
    <FirmName>FIRM NAME, INC</FirmName> 
    <Address2>123 MAIN ST</Address2> 
    <City>SOME PLACE</City> 
    <State>CA</State> 
    <Zip5>90028</Zip5> 
    <Zip4>1467</Zip4> 
  </Address>
  <Address ID="3">
     <FirmName>FIRM NAME, INC</FirmName> 
     <Address2>123 MAIN ST</Address2> 
     <City>SOME PLACE</City> 
     <State>CA</State> 
     <Zip5>90028</Zip5> 
     <Zip4>1467</Zip4> 
  </Address>
  <Address ID="4">
    <Error>
      <Number>-2147219401</Number> 
      <Source>SOURCE INFO HERE</Source> 
      <Description>Address Not Found.</Description> 
      <HelpFile /> 
      <HelpContext>1000440</HelpContext> 
    </Error>
  </Address>
</AddressValidateResponse>

我需要创建两个列表。一个有效地址,一个有错误块。在上面的示例中,第一个列表将包含3个地址,第二个列表将包含两个地址。不确定过滤器如何正确查询。感谢。

我能够使用以下内容,但我怀疑有一种更有效的方法可以做同样的事情:

    var errors = from d in xDoc.Descendants("Address")
                         from e in d.Elements("Error")
                         where e.Element("Description").Value.Trim().ToUpper().Contains("ADDRESS NOT FOUND")
                         select new AddressObject
                         {
                             Order = (int)d.Attribute("ID"),
                             StreetAddress = "NO MATCH FOUND",
                             OtherAddress = String.Empty,
                             City = String.Empty,
                             State = String.Empty,
                             ZipCode = String.Empty,
                             ZipPlus4 = String.Empty
                         };

            errorList = errors.ToList();

    var addresses = from a in xDoc.Descendants("Address")
                        from b in a.Elements("FirmName")
                        where b.Value != String.Empty
                        select new AddressObject
                        {
                            Order = (int)a.Attribute("ID"),
                            StreetAddress = (string)a.Element("Address2") ?? String.Empty,
                            OtherAddress = (string)a.Element("Address1") ?? String.Empty,
                            City = (string)a.Element("City") ?? String.Empty,
                            State = (string)a.Element("State") ?? String.Empty,
                            ZipCode = (string)a.Element("Zip5") ?? String.Empty,
                            ZipPlus4 = (string)a.Element("Zip4") ?? String.Empty
                        };

    validList = addressess.ToList();

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情 - 似乎应该有一种更有效的方式,但我现在无法理解......

string xmlContent = @"<?xml version=""1.0"" ?> 
<AddressValidateResponse>
  <Address ID=""0"">
    <FirmName>FIRM NAME, INC</FirmName> 
    <Address2>123 MAIN ST</Address2> 
    <City>SOME PLACE</City> 
    <State>CA</State> 
    <Zip5>90028</Zip5> 
    <Zip4>1467</Zip4> 
  </Address>
  <Address ID=""1"">
    <Error>
      <Number>-2147219401</Number> 
      <Source>SOURCE INFO HERE</Source> 
      <Description>Address Not Found.</Description> 
      <HelpFile /> 
      <HelpContext>1000440</HelpContext> 
    </Error>
  </Address>
  <Address ID=""2"">
    <FirmName>FIRM NAME, INC</FirmName> 
    <Address2>123 MAIN ST</Address2> 
    <City>SOME PLACE</City> 
    <State>CA</State> 
    <Zip5>90028</Zip5> 
    <Zip4>1467</Zip4> 
  </Address>
  <Address ID=""3"">
     <FirmName>FIRM NAME, INC</FirmName> 
     <Address2>123 MAIN ST</Address2> 
     <City>SOME PLACE</City> 
     <State>CA</State> 
     <Zip5>90028</Zip5> 
     <Zip4>1467</Zip4> 
  </Address>
  <Address ID=""4"">
    <Error>
      <Number>-2147219401</Number> 
      <Source>SOURCE INFO HERE</Source> 
      <Description>Address Not Found.</Description> 
      <HelpFile /> 
      <HelpContext>1000440</HelpContext> 
    </Error>
  </Address>
</AddressValidateResponse>";

XDocument doc = XDocument.Parse(xmlContent);

var errors = doc.Descendants("Address").Where(a => a.Descendants("Error").Count() > 0).ToList();
var real = doc.Descendants("Address").Where(a => a.Descendants("Error").Count() == 0).ToList();

基本上,您检查每个<Address> XElement它包含名称为Error的子元素数量 - 0表示“真实”地址,&gt; 0表示错误输入。