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();
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 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这样的方法 - 似乎应该有一种更有效的方法,但我现在无法弄清楚...
基本上,您检查每个
XElement 有多少个它包含的名称为
Error
的子元素 - 0 表示“真实”地址,> 0 表示有错误条目。You could try something like this - seems there ought to be a more efficient way, but I can't figure it out right now...
Basically, you check for each
<Address>
XElement how many sub-elements with a name ofError
it contains - 0 means a "real" address, > 0 means an error entry.