获取过滤列表;来自 XML

发布于 2024-12-19 09:05:50 字数 1949 浏览 1 评论 0原文

我正在使用以下代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

审判长 2024-12-26 09:05:50

如果您有权访问 .net >= 3.5,则应考虑使用 Linq to Xml

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

那些过往 2024-12-26 09:05:50

下面是考虑 cid 参数过滤器的 LINQ-to-XML 查询示例。如果您将空字符串放入 cidFilter 中,查询将返回所有条目:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文