Linq to XML query problem
Here is my 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>
I need to create two lists. One with valid addresses and one with the errors blocks. In the above example, the first list would contain 3 addresses and the second list would contain two. Not sure how filter query properly. Thanks.
I was able to get it to work with the following but I suspect there is a more efficient way to do the same thing:
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开发者_JS百科 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();
You could try something like this - seems there ought to be a more efficient way, but I can't figure it out right now...
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();
Basically, you check for each <Address>
XElement how many sub-elements with a name of Error
it contains - 0 means a "real" address, > 0 means an error entry.
精彩评论