Adds a default namespace to a simple XPath expression.
Namespace: CleanCode.XmlAssembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
public static string AddDefaultNamespace(
string defaultNS,
string xpathExpression
)
Public Shared Function AddDefaultNamespace ( _
defaultNS As String, _
xpathExpression As String _
) As String
Public Shared Function AddDefaultNamespace ( _
defaultNS As String, _
xpathExpression As String _
) As String
public:
static String^ AddDefaultNamespace(
String^ defaultNS,
String^ xpathExpression
)
Return Value
XPath expression with namespace prefix
on each path component.
As an XML parser does not care about the lexical difference
between aa and bb when specifying
e.g. xmlns:aa="a URI" and xmlns:bb="the same URI".
You may introduce any namespace designation you please
once you map it to the same URI.
(See
Select(XPathExpression) for details.)
Here is an example usage when using an XPathNavigator:
XmlNamespaceManager nsMgr =
new XmlNamespaceManager(navigator.NameTable);
nsMgr.AddNamespace(defaultNS, "some URI here");
XPathExpression expr = navigator.Compile(
AddDefaultNamespace(defaultNS, targetXpathExpr));
expr.SetContext(nsMgr);
XmlNamespaceManager nsMgr =
new XmlNamespaceManager(navigator.NameTable);
nsMgr.AddNamespace(defaultNS, "some URI here");
XPathExpression expr = navigator.Compile(
AddDefaultNamespace(defaultNS, targetXpathExpr));
expr.SetContext(nsMgr);
XmlNamespaceManager nsMgr =
new XmlNamespaceManager(navigator.NameTable);
nsMgr.AddNamespace(defaultNS, "some URI here");
XmlNodeList nodes = doc.SelectNodes(
AddDefaultNamespace(defaultNS, targetXpathExpr), nsMgr);
XmlNamespaceManager nsMgr =
new XmlNamespaceManager(navigator.NameTable);
nsMgr.AddNamespace(defaultNS, "some URI here");
XmlNodeList nodes = doc.SelectNodes(
AddDefaultNamespace(defaultNS, targetXpathExpr), nsMgr);
When using an XmlDocument the procedure is simpler:
Note that this does not work with XPath expressions that
are qualified with predicates,
i.e.
//root/elem[x/y/@attr='abc'].