C #, XML request question

I have many XML files containing the same XML document, but with different values. But the structure for each file is the same.

Inside this file, I have a datetime field.

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

I am using C # and .net v2. Should I use XML objects to achieve this or text file search procedures?

Some code examples would be good ... or just a general theory, everything could help, thanks ...

+3
source share
3 answers

, . , XML- XPath , , , .

: XPathDocument, XmlDocument XPath

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

- ( ):

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
    }
}

. XPathDocument URI/path, , , XPath. , .

+2
+1

If at all it is possible to upgrade to C # 3.0 / .NET 3.5, LINQ-to-XML will be the easiest option.

With .NET 2.0, you are stuck with either XML objects or XSL.

+1
source

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


All Articles