XPath Query with XmlNode.SelectSingleNode

While trying to parse SBML biological models on my ASP.NET project, I faced with a strange behavior of Select method of XmlNode.

In SBML, users have to define a namespace for the SBML version and level. However, in general for the remaining of the RDF file which defines species, reactions, etc., users do not use any namespace and rely on default namespace.

Nonetheless, Select method of XmlNode needs a namespace to invoke a XPath query on RDF file. You need to define a namespace for your XmlDocument like the following;

XPathDocument x = new XPathDocument(new StringReader(xmldoc.OuterXml));
XPathNavigator foo = x.CreateNavigator();
foo.MoveToFollowing(XPathNodeType.Element);

// Add the namespace.
var NSDICT = foo.GetNamespacesInScope(XmlNamespaceScope.All);

var nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
foreach (KeyValuePair pair in NSDICT)
{
if (String.IsNullOrEmpty(pair.Key))
{
nsmgr.AddNamespace("pcp", pair.Value); // Add PathCase prefix, if key is empty.
}
else
{
nsmgr.AddNamespace(pair.Key, pair.Value);
}
}

Comments

Popular posts from this blog

3 Must Haves of a Status Report

AWS encryption chart (SSE-S3 vs SSE-KMS vs SSE-C)

Configure hosts File in Android