简体   繁体   中英

How to add an additional filter to this XML

I have the following C# code to parse an xml document :

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;
            }
        }
    );

I now need to add to the linq query to further filter the data. The data point that is now being filtered is the county information in the address portion of the the xml. I have tried using this code but it will not compile.

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 =>
        {

As you can seen I tried adding an and clause to the linq statement with no luck.

I was able to get this line of code to compile but now I am not getting any records

                            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 =>

You need to learn to segment your code. It is very hard to read a one-liner like that.

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 =>

This is the 2nd line, the one you are trying to modify :

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 =>

You need to chain your where clause or combine them within the same where clause :

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 =>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM