解析与ASP.NET(C#)多个XML文件,并返回那些与特定元素 [英] Parse multiple XML files with ASP.NET (C#) and return those with particular element

查看:116
本文介绍了解析与ASP.NET(C#)多个XML文件,并返回那些与特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。

我正在寻找一种方法来解析与ASP.NET(C#)特定目录的一些XML文件。我希望能够返回从特定元素的内容,但在这之前,需要找到那些有一个元素之间有一定的价值。

I'm looking for a way to parse a number of XML files in a particular directory with ASP.NET (C#). I'd like to be able to return content from particular elements, but before that, need to find those that have a certain value between an element.

XML文件示例1:

<file>
    <title>Title 1</title>
    <someContent>Content</someContent>
    <filter>filter</filter>
</file>

XML文件示例2:

Example XML file 2:

<file>
    <title>Title 2</title>
    <someContent>Content</someContent>
    <filter>filter, different filter</filter>
</file>

例病例1:

给我拥有的过滤器过滤器的所有XML。

Give me all XML that has a filter of 'filter'.

事例2:

给我拥有的标题1标题所有的XML。

Give me all XML that has a title of 'Title 1'.

看,似乎这应该是可能的LINQ,但我只看到了如何做到这一点的例子时有一个XML文件,而不是当有倍数,如在这种情况下。

Looking, it seems this should be possible with LINQ, but I've only seen examples on how to do this when there is one XML file, not when there are multiples, such as in this case.

我想preFER,这将在服务器端完成的,所以,我可以为此缓存。

I would prefer that this be done on the server-side, so that I can cache on that end.

从.NET Framework的任何版本的功能都可以使用。

Functionality from any version of the .NET Framework can be used.

谢谢!

〜詹姆斯

推荐答案

如果您使用的是.NET 3.5,这是非常容易使用LINQ:

If you are using .Net 3.5, this is extremely easy with LINQ:

//get the files
XElement xe1 = XElement.Load(string_file_path_1);
XElement xe2 = XElement.Load(string_file_path_2);

//Give me all XML that has a filter of 'filter'.
var filter_elements1 = from p in xe1.Descendants("filter") select p;
var filter_elements2 = from p in xe2.Descendants("filter") select p;
var filter_elements = filter_elements1.Union(filter_elements2);

//Give me all XML that has a title of 'Title 1'.
var title1 = from p in xe1.Descendants("title") where p.Value.Equals("Title 1") select p;
var title2 = from p in xe2.Descendants("title") where p.Value.Equals("Title 1") select p;
var titles = title1.Union(title2);

这都可以写速记,让你结果在短短的4行总:

This can all be written shorthand and get you your results in just 4 lines total:

XElement xe1 = XElement.Load(string_file_path_1);
XElement xe2 = XElement.Load(string_file_path_2);
var _filter_elements = (from p1 in xe1.Descendants("filter") select p1).Union(from p2 in xe2.Descendants("filter") select p2);
var _titles = (from p1 in xe1.Descendants("title") where p1.Value.Equals("Title 1") select p1).Union(from p2 in xe2.Descendants("title") where p2.Value.Equals("Title 1") select p2);

这些都将IEnumerable的名单,所以他们是超级容易的工作:

These will all be IEnumerable lists, so they are super easy to work with:

foreach (var v in filter_elements)
    Response.Write("value of filter element" + v.Value + "<br />");

LINQ规则!

LINQ rules!

这篇关于解析与ASP.NET(C#)多个XML文件,并返回那些与特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆