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();

// 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.
nsmgr.AddNamespace(pair.Key, pair.Value);


Popular posts from this blog

Space Character Problem on IE 6, 7, and 8

Configure hosts File in Android

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