0
我有下面的C#代碼解析XML文檔:如何額外的過濾器添加到該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 =>
謝謝@Xiaoy,因爲你的建議使我的代碼更易於閱讀,並解決了我的問題。 – Perry