Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, XML Query Question

Tags:

c#

xml

I have tons of XML files all containing a the same XML Document, but with different values. But the structure is the same for each file.

Inside this file I have a datetime field.

What is the best, most efficient way to query these XML files? So I can retrieve for example... All files where the datetime field = today's date?

I'm using C# and .net v2. Should I be using XML objects to achieve this or text in file search routines?

Some code examples would be great... or just the general theory, anything would help, thanks...

like image 838
JL. Avatar asked Dec 04 '25 17:12

JL.


1 Answers

This depends on the size of those files, and how complex the data actually is. As far as I understand the question, for this kind of XML data, using an XPath query and going through all the files might be the best approach, possibly caching the files in order to lessen the parsing overhead.

Have a look at: XPathDocument, XmlDocument classes and XPath queries

http://support.microsoft.com/kb/317069

Something like this should do (not tested though):

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
// if required, add your namespace prefixes here to nsmgr
XPathExpression expression = XPathExpression.Compile("//element[@date='20090101']", nsmgr); // your query as XPath
foreach (string fileName in Directory.GetFiles("PathToXmlFiles", "*.xml")) {
    XPathDocument doc;
    using (XmlTextReader reader = new XmlTextReader(fileName, nsmgr.NameTable)) {
        doc = new XPathDocument(reader);
    }
    if (doc.CreateNavigator().SelectSingleNode(expression) != null) {
        // matching document found
    }
}

Note: while you can also load a XPathDocument directly from a URI/path, using the reader makes sure that the same nametable is being used as the one used to compile the XPath query. If a different nametable was being used, you'd not get results from the query.

like image 136
Lucero Avatar answered Dec 07 '25 14:12

Lucero