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

6 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

  3. Hi Divesh Bora,
    Its nice Article, I have a one rewrites, which I have to do it from NAV 2009 to BC13. you know NAV 2009 has Automation Varilables, and those automation has to rewritten in BC 13 or BC 14.
    Question here is :
    1. If I am going with On Prem CAL -> Can I user Dot Net Variables?
    2. Do you have any AL file (Repository) which its Creating Sample Request and Response.

    Like

Leave a Reply to mohsinjanjua Cancel 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