XMLNamespaceManager
XML namespaces are also important when parsing XML documents with XPath. To
specify the namespaces that your XPath expressions refer to, use an XMLNamespaceManager
object. This example process checks, whether an XML response
sent by the OLAP contains any errors:
bool OlapResponseHasErrors(XMLDocument responseDoc)
{
string OlapNamespaceURI = "http://www.misag.com";
XMLNamespaceManager namespaceManager = XMLCreateNamespaceManager();
XMLAddNamespace(namespaceManager, "Alea", OlapNamespaceURI);
if (XMLXPathHasElement(XMLGetRootElement(responseDoc), "/Alea:Document/Alea:Request/Alea:Error", namespaceManager))
{
WriteLine("An error occurred.");
return false;
}
else
{
WriteLine("Everything went fine.");
return true;
}
}
If the request could not be processed, the response document contains an element named <Alea:Error>
. To specify such an element using XPath, you must tell the XPath engine how to interpret the Alea
namespace. That is the purpose of the XMLNamespaceManager
object in the preceding code example. Using the XMLAddNamespace
function, you can add as many namespaces as necessary.