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>
Below an example of LINQ-to-XML query which consider
cidparemeter filter. If you put empty string incidFilterthe query would return all entries: