我有以下C#代码来解析xml文档:
XDocument.Load(ConfigurationManager.AppSettings["XDocumentLoad"])
.Root
.Elements(j + "RegisteredOffenders")
.ToList()
.ForEach(element =>
{
//build out the xml namespace for the data parse
var ns = element.GetDefaultNamespace();
var role = element.Element(ns + "RoleOfPerson");
var PersonName = role.Element(ns + "PersonName");
var offender = element.Element(j + "RegisteredOffenderIdentification");
var id = element.Attribute(s + "id").Value;
//This is an inner loop that gets all the addresss for a person and writes the info to the temp strings declared above.
element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Where(a => a.Element(se + "offenderIdRef").Value == id).ToList().ForEach(ad =>
{
string aCode = ad.Element(sc + "AddressCategoryCode").Value;
switch (aCode.ToUpper())
{
case "TEMP":
string TempAddressCode = ad.Element(sc + "AddressCategoryCode").Value;
string TempStreet = ad.Element(ns + "LocationStreet").Element(ns + "StreetFullText").Value;
string Tempcity = ad.Element(ns + "LocationCityName").Value;
string Tempstate = ad.Element(sc + "LocationUSStateCode").Value;
string TempzipOne = ad.Element(ns + "LocationPostalCode").Value;
string TempzipTwo = ad.Element(ns + "LocationPostalExtensionCode").Value;
TempLocation = string.Format("{0},{1},{2},{3},{4}", TempStreet, Tempcity, TempStreet, TempzipOne, TempzipTwo);
break;
case "PERM":
string PermAddressCode = ad.Element(sc + "AddressCategoryCode").Value;
string PermStreet = ad.Element(ns + "LocationStreet").Element(ns + "StreetFullText").Value;
string Permcity = ad.Element(ns + "LocationCityName").Value;
string PermCounty = ad.Element(sc + "LocationNonFLCounty").Value;
string Permstate = ad.Element(sc + "LocationUSStateCode").Value;
string PermzipOne = ad.Element(ns + "LocationPostalCode").Value;
string PermzipTwo = ad.Element(ns + "LocationPostalExtensionCode").Value;
PermLocation = string.Format("{0},{1},{2},{3},{4},{5}", PermStreet, Permcity, PermCounty, Permstate, PermzipOne, PermzipTwo);
break;
case "TRANS":
string TransAddressCode = ad.Element(sc + "AddressCategoryCode").Value;
string TransStreet = ad.Element(ns + "LocationStreet").Element(ns + "StreetFullText").Value;
string Transcity = ad.Element(ns + "LocationCityName").Value;
string Transstate = ad.Element(sc + "LocationUSStateCode").Value;
string TranszipOne = ad.Element(ns + "LocationPostalCode").Value;
string TranszipTwo = ad.Element(ns + "LocationPostalExtensionCode").Value;
TransLocation = string.Format("{0},{1},{2},{3},{4}", TransStreet, Transcity, TransStreet, TranszipOne, TranszipTwo);
break;
}
}
);
我现在需要添加到linq查询以进一步过滤数据。现在正在过滤的数据点是xml的地址部分中的县信息。我尝试过使用此代码,但无法编译。
XDocument.Load(ConfigurationManager.AppSettings["XDocumentLoad"])
.Root
.Elements(j + "RegisteredSexOffender")
.ToList()
.ForEach(element =>
{
//build out the xml namespace for the data parse
var ns = element.GetDefaultNamespace();
var role = element.Element(ns + "RoleOfPerson");
var PersonName = role.Element(ns + "PersonName");
var offender = element.Element(j + "RegisteredOffenderIdentification");
var id = element.Attribute(s + "id").Value;
//This is an inner loop that gets all the addresss for a person and writes the info to the temp strings declared above.
element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Where(a => a.Element(se + "offenderIdRef").Value == id) && element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Elemetns(sc + "LocationNonFLCounty").Value=="ORANGE").ToList().ForEach(ad =>
{
正如您所见,我尝试在linq语句中添加一个和子句,但没有运气。
我能够编译这行代码,但现在我没有得到任何记录
element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Where(a => a.Element(se + "offenderIdRef").Value == id && a.Element(sc + "LocationNonFLCounty").Value == "Orange").ToList().ForEach(ad =>
答案 0 :(得分:0)
您需要学会细分代码。很难像这样阅读单行。
element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Where(a => a.Element(se + "offenderIdRef").Value == id).ToList().ForEach(ad =>
// this line has 334 characters...
element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Where(a => a.Element(se + "offenderIdRef").Value == id) && element.Document.Root.Element(se + "SopsOffenderAddressList").Elements(se + "SopsOffenderAddress").Elemetns(sc + "LocationNonFLCounty").Value=="ORANGE").ToList().ForEach(ad =>
这是你试图修改的第二行:
element.Document.Root
.Element(se + "SopsOffenderAddressList")
.Elements(se + "SopsOffenderAddress")
.Where(a => a.Element(se + "offenderIdRef").Value == id)
&& // this is not how you 'add' another 'filter'(where clause)
element.Document.Root
.Element(se + "SopsOffenderAddressList")
.Elements(se + "SopsOffenderAddress")
.Elemetns(sc + "LocationNonFLCounty").Value=="ORANGE") // I think you mean to compare a *single* element to "ORANGE"
.ToList().ForEach(ad =>
您需要链接where
子句或将它们组合在同一where
子句中:
element.Document.Root
.Element(se + "SopsOffenderAddressList")
.Elements(se + "SopsOffenderAddress")
// chaining (pick one)
.Where(a => a.Element(se + "offenderIdRef").Value == id)
.Where(a => a.Element(sc + "LocationNonFLCounty").Value == "ORANGE")
// combined (pick one)
.Where(a =>
a.Element(se + "offenderIdRef").Value == id &&
a.Element(sc + "LocationNonFLCounty").Value == "ORANGE")
.ToList().ForEach(ad =>