获取过滤列表;来自 XML
我正在使用以下代码从 xml 文件获取列表 -
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);
}
}
但是,我还需要一种过滤记录的方法,可以通过 -
- 不。从 xml 文件中获取的记录数
- 按特定节点值过滤
因此,上述方法的签名将更改为 -
public static List<T> GetListFromXml<T>(string filePath,
int listCount,
string filterbyNode,
string filterByValue);
请指导我是否可以直接从 XML 文件进行过滤,或者我应该从返回的列表中进行过滤?
PS
上面提到的 xml 文件也是使用代码创建的 -
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);
}
}
为什么我将从数据库返回的自定义集合保存到 xml 文件 - 因为我只想一次只处理一批记录。
和 XML 文件片段(从上面的代码生成)-
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有权访问 .net >= 3.5,则应考虑使用 Linq to Xml
If you have access to .net >= 3.5, you should consider using Linq to Xml
下面是考虑
cid
参数过滤器的 LINQ-to-XML 查询示例。如果您将空字符串放入cidFilter
中,查询将返回所有条目:Below an example of LINQ-to-XML query which consider
cid
paremeter filter. If you put empty string incidFilter
the query would return all entries: