Get filtered List<T> from XML

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>

Answers


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

If you have access to .net >= 3.5, you should consider using Linq to Xml


Need Your Help

Hide window task bar code not working on iis7.5

c# asp.net .net

i am working on hide task bar of window this code working fine on visual studo 2010 but not working on IIS 7.5 and not give any error .Please tell me why this code not working on iis7.5 (window 7)...

Fast combinatoric generator in Python

python performance generator combinatorics

As part of a large project in python, I need a fast generator function that produces all possible sets of non-negative integer numbers smaller than n, such that each set has at most s elements and ...