What is the XPath (in C# API to XDocument.XPathSelectElements(xpath, nsman) if it matters) to query all MyNodes from this document?
I like @mads-hansen, his answer, so well that I wrote these general-purpose utility-class members:
///
/// Gets the into a local-name() , XPath-predicate query.
///
/// Name of the child element.
///
public static string GetLocalNameXPathQuery(string childElementName)
{
return GetLocalNameXPathQuery(namespacePrefixOrUri: null, childElementName: childElementName, childAttributeName: null);
}
///
/// Gets the into a local-name() , XPath-predicate query.
///
/// The namespace prefix or URI.
/// Name of the child element.
///
public static string GetLocalNameXPathQuery(string namespacePrefixOrUri, string childElementName)
{
return GetLocalNameXPathQuery(namespacePrefixOrUri, childElementName, childAttributeName: null);
}
///
/// Gets the into a local-name() , XPath-predicate query.
///
/// The namespace prefix or URI.
/// Name of the child element.
/// Name of the child attribute.
///
///
/// This routine is useful when namespace-resolving is not desirable or available.
///
public static string GetLocalNameXPathQuery(string namespacePrefixOrUri, string childElementName, string childAttributeName)
{
if (string.IsNullOrEmpty(childElementName)) return null;
if (string.IsNullOrEmpty(childAttributeName))
{
return string.IsNullOrEmpty(namespacePrefixOrUri) ?
string.Format("./*[local-name()='{0}']", childElementName)
:
string.Format("./*[namespace-uri()='{0}' and local-name()='{1}']", namespacePrefixOrUri, childElementName);
}
else
{
return string.IsNullOrEmpty(namespacePrefixOrUri) ?
string.Format("./*[local-name()='{0}']/@{1}", childElementName, childAttributeName)
:
string.Format("./*[namespace-uri()='{0}' and local-name()='{1}']/@{2}", namespacePrefixOrUri, childElementName, childAttributeName);
}
}