VS Code XML DOM Management – Part 1

This is my first post on VS Code AL Extension 2.0.

I was working with SOAP Web services and was searching if there are some existing functions which would ease reading of the XML response / XML Text in AL similar to XML DOM Management in C/AL.

We have XML DOM Management codeunit in C/AL, but we cannot use most of the functions from the VS Code Extension as it requires a DotNet object as input. So thought of creating a similar codeunit with the XML Class available in VS Code AL Extension. I have created only a few sets of functions and thought of sharing the same. Meanwhile, I will check & create more functions when I have some time.

Below is the code:

codeunit 50100 “XML DOM Mgt.
{
trigger OnRun();
begin
end;

procedure AddAttribute(pXMLNode: XmlNode; pName: Text; var pValue: Text): Boolean
begin

IF pXMLNode.AsXmlElement.IsEmpty then
exit(false);
pXMLNode.AsXmlElement.SetAttribute(pName, pValue);
end;

procedure FindNode(pXMLRootNode: XmlNode; pNodePath: Text; var pFoundXMLNode: XmlNode): Boolean
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(false);
IF pXMLRootNode.SelectSingleNode(pNodePath, pFoundXMLNode) then
exit(true);
end;

procedure FindNodeWithNameSpace(pXMLRootNode: XmlNode; pNodePath: Text; pPrefix: Text; pNamespace: Text; var pFoundXMLNode: XmlNode): Boolean
var
lXmlNsMgr: XmlNamespaceManager;
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(false);
lXmlNsMgr.NameTable(pXMLRootNode.AsXmlDocument.NameTable);
lXMLNsMgr.AddNamespace(pPrefix, pNamespace);

IF pXMLRootNode.SelectSingleNode(pNodePath, lXmlNsMgr, pFoundXMLNode) then
Exit(true);
end;

procedure FindNodesWithNameSpace(pXMLRootNode: XmlNode; pXPath: Text; pPrefix: Text; pNamespace: Text; var pFoundXmlNodeList: XmlNodeList): Boolean
var
lXmlNode: XmlNode;
lXmlNsMgr: XmlNamespaceManager;
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(false);
lXmlNsMgr.NameTable(pXMLRootNode.AsXmlDocument.NameTable);
lXMLNsMgr.AddNamespace(pPrefix, pNamespace);
exit(FindNodesWithNamespaceManager(pXMLRootNode, pXPath, lXmlNsMgr, pFoundXmlNodeList));
end;

procedure FindNodesWithNamespaceManager(pXMLRootNode: XmlNode; pXPath: Text; pXmlNsMgr: XmlNamespaceManager; var pFoundXmlNodeList: XmlNodeList): Boolean
var
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(false);
IF not pXMLRootNode.SelectNodes(pXPath, pXmlNsMgr, pFoundXmlNodeList) then
exit(false);

IF pFoundXmlNodeList.Count = 0 then
exit(false);
exit(true);
end;

procedure FindNodeXML(pXMLRootNode: XmlNode; pNodePath: Text): Text
var
lXmlNode: XmlNode;
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(”);
IF pXMLRootNode.SelectSingleNode(pNodePath, lXmlNode) then
Exit(lXmlNode.AsXmlElement.InnerXml);
end;

procedure FindNodeText(pXMLRootNode: XmlNode; pNodePath: Text): Text
var
lXmlNode: XmlNode;
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(”);
IF pXMLRootNode.SelectSingleNode(pNodePath, lXmlNode) then
Exit(lXmlNode.AsXmlElement.InnerText);
end;

procedure FindNodeTextWithNameSpace(pXMLRootNode: XmlNode; pNodePath: Text; pPrefix: Text; pNamespace: Text): Text
var
lXmlNode: XmlNode;
lXmlNsMgr: XmlNamespaceManager;
begin
IF pXMLRootNode.AsXmlElement.IsEmpty then
exit(”);
lXmlNsMgr.NameTable(pXMLRootNode.AsXmlDocument.NameTable);
lXMLNsMgr.AddNamespace(pPrefix, pNamespace);

IF pXMLRootNode.SelectSingleNode(pNodePath, lXmlNsMgr, lXmlNode) then
Exit(lXmlNode.AsXmlElement.InnerText);
end;

procedure LoadXMLDocumentFromText(pXMLText: Text; var pXMLDocument: XmlDocument)
begin
IF pXMLText = ” then
exit;
XmlDocument.ReadFrom(pXMLText, pXMLDocument);
end;

procedure LoadXMLNodeFromText(pXMLText: Text; var pXMLRootNode: XmlNode)
var
lXmlDocument: XmlDocument;
begin
LoadXMLDocumentFromText(pXMLText, lXmlDocument);
pXMLRootNode := lXmlDocument.AsXmlNode;
end;
}

Hope this post is useful. Please let me know your comments. Thanks

Advertisements

5 thoughts on “VS Code XML DOM Management – Part 1

  1. Pingback: VS Code XML DOM Management – Part 2 | Divesh Bora – MSD NAV Blog

  2. When trying to call FindNodetext() method, I get this run time error:

    Unable to convert from Microsoft.Dynamics.Nav.Runtime.NavXmlDocument to Microsoft.Dynamics.Nav.Runtime.NavXmlElement.

    How can this be fixed?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s