XPathWrangler AddDefaultNamespace Method CleanCode C# Libraries v1.2.03 API
Adds a default namespace to a simple XPath expression.

Namespace: CleanCode.Xml
Assembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
Syntax

public static string AddDefaultNamespace(
	string defaultNS,
	string xpathExpression
)

Return Value

XPath expression with namespace prefix on each path component.
Remarks

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 OnlineSelect(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");
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'].
See Also