Parse multiple XML files using ASP.NET (C #) and return them with a specific element

Hey.

I am looking for a way to parse multiple XML files in a specific directory using ASP.NET (C #). I would like to be able to return content from specific elements, but before that I need to find those that have a specific value between the element.

Example XML file 1:

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

Example XML file 2:

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

Example 1:

Give me all the XML that has the filter 'filter'.

Example 2:

Give me all the XML that has the title "Title 1".

Looking, it looks like this should be possible with LINQ, but I saw only examples of how to do this when there is one XML file, and not when there are multiple ones, for example, in this case.

I would prefer it to be done on the server side so that I can cache for this purpose.

.NET Framework.

!

~

+2
4

.Net 3.5, 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 :

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 , :

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

LINQ!

+7

, , .

, XMLContentEnumerator: IEnumerable. , LINQ, :

var xc = new XMLContentEnumerator(@"C:\dir");

var filesWithHello = xc.Where(x => x.title.Contains("hello"));

, , .

+2

Here is one way to use Framework 2.0. You can do this cleaner using regular expressions rather than a simple string test. You can also try compiling your XPath expressions if you need to compress performance more.

static void Main(string[] args)
{
    string[] myFiles = { @"C:\temp\XMLFile1.xml", 
                         @"C:\temp\XMLFile2.xml", 
                         @"C:\temp\XMLFile3.xml" };
    foreach (string file in myFiles)
    {
        System.Xml.XPath.XPathDocument myDoc = 
            new System.Xml.XPath.XPathDocument(file);
        System.Xml.XPath.XPathNavigator myNav = 
            myDoc.CreateNavigator();

        if(myNav.SelectSingleNode("/file/filter[1]") != null &&
            myNav.SelectSingleNode("/file/filter[1]").InnerXml.Contains("filter"))
            Console.WriteLine(file + " Contains 'filter'");

        if (myNav.SelectSingleNode("/file/title[1]") != null &&
            myNav.SelectSingleNode("/file/title[1]").InnerXml.Contains("Title 1"))
            Console.WriteLine(file + " Contains 'Title 1'");
    }

    Console.ReadLine();
}
+2
source

Source: https://habr.com/ru/post/1718281/


All Articles