Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6888427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:00:51+00:00 2026-05-27T06:00:51+00:00

I am using following code to get List from xml file – public static

  • 0

I am using following code to get List from xml file –

    public static List<T> GetListFromXml<T>(string filePath)
    {
        using (TextReader reader = new StreamReader(filePath))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
            return (List<T>) serializer.Deserialize(reader);
        }
    }

However, I also need a way of filtering records where I can filter list by –
– no. of records to take from xml file
– filter by a particular node value
So signature of above method will change to –

    public static List<T> GetListFromXml<T>(string filePath, 
                                            int listCount, 
                                            string filterbyNode, 
                                            string filterByValue);

Please guide me if I can filter directly from XML file or I should filter from returned list?


P.S.

The xml file mentioned above is also created from code using –

    public static void WriteListToXml<T>(List<T> list, string filePath)
    {
        using (TextWriter writer = new StreamWriter(filePath))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
            serializer.Serialize(writer, list);
        }
    }

Why I am saving my custom collection returned from database to xml file – because I want to process only a batch of records at a time.

and XML file snippet (generated from above code) –

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClassifiedLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CLocation>
    <CId>5726</CId>
    <Long>0</Long>
    <Lat>0</Lat>
    <Postcode>ZZ1 5ZZ</Postcode>
    <Street />
    <Town />
  </CLocation>
  <CLocation>
    <CId>5736</CId>
    <Long>0</Long>
    <Lat>0</Lat>
    <Postcode>ZZ1 5ZZ</Postcode>
    <Street />
    <Town />
  </CLocation>
</ArrayOfClassifiedLocation>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:00:52+00:00Added an answer on May 27, 2026 at 6:00 am

    Below an example of LINQ-to-XML query which consider cid paremeter filter. If you put empty string in cidFilter the query would return all entries:

    // you can use Parse() method:
    // XDocument loaded = XDocument.Parse(xmlString);
    XDocument loaded = XDocument.Load("c:\\temp\\testxl.xml");
    
    // return only entry fith CID == 5726
    string cidFilter = "5726";
    var filteredItems =
    loaded.Descendants("CLocation")
        .Select((w, i) =>
                new
                    {
                        Index = i,
                        CID = w.Descendants("CId").FirstOrDefault().Value,
                        PostCode = w.Descendants("Postcode").FirstOrDefault().Value
                    })
        .Where(item => String.IsNullOrEmpty(cidFilter) || item.CID == cidFilter)
        .ToList();
    
    int itemsCount = filteredItems.Count();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the error list iterator not dereferencable when using the following code: bool
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I'm using the following code to get an array with all sub directories from
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Im using the following code to get a bunch of information about employees from
Using the following code to display a list of friends from my twitter profile.
Currently for my blog I get a list of years/dates using the following code:
I'm using System.Xml to get attributes from my xml file . It seems that
I have the following code to get a list of changed files from Subversion
I'm using the following code to get the members of a group on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.