-
Notifications
You must be signed in to change notification settings - Fork 1
/
XPathProcessor.cs
32 lines (26 loc) · 1.01 KB
/
XPathProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
namespace XPathQuery
{
public static class XPathProcessor
{
public static XPathProcessorResult Process(string xmlFile, string xpath, IEnumerable<Namespace> namespaces)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XPathNavigator nav = doc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
foreach (Namespace ns in namespaces)
nsMgr.AddNamespace(ns.Prefix, ns.URI);
XPathExpression expr = XPathExpression.Compile(xpath, nsMgr);
object result;
if (expr.ReturnType == XPathResultType.NodeSet)
result = doc.SelectNodes(xpath, nsMgr);
else
result = nav.Evaluate(expr);
XPathProcessorResult processorResult = new XPathProcessorResult(result, expr.ReturnType);
return processorResult;
}
}
}