Return many variables from an xml file

Discussion in 'Call Flow Designer' started by SEBASTIEN CARROUGET, Feb 21, 2018.

Thread Status:
Not open for further replies.
  1. SEBASTIEN CARROUGET

    Joined:
    Jan 31, 2018
    Messages:
    2
    Likes Received:
    0
    Hi,

    For may callflow I check in a database if caller is know as customer and create a xml file with many information about his last order.

    In CFD I can read the remote xml file created but it seems it ca return only one thing : true or false.

    I need to initiate many variables with datas stored in xml file to perform some action and speak to text in CFD.

    Exemple of the xml file :
    <appels>
    <appel action="isclient" datetime="21-02-2018 15-36-41" called="XXXXXXXXX" number="XXXXXX" ext="800" sw="2" client="47596" cmd="53646" cmdday="20" cmdmonth="febrary" cmdyear="2018" cmdh="20" cmdm="11" payer="1" payeday="20" payemonth="febrary" payeyear="2018" payeh="20" payem="14" payemethod="CB" payeconf="SINGLE" cmdindic="CMD in BE" cmdstat="5"/>
    </appels>

    So I wrote the xml reader like this :

    using System.Xml;

    namespace TextFileValidator
    {
    public class Validator
    {
    public bool ValidateXML(string fileContent)
    {
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(fileContent);

    XmlNodeList customerElementList = xmlDocument.GetElementsByTagName("appel");
    foreach (XmlNode xmlNode in customerElementList)
    {

    string cmd = xmlNode.Attributes["cmd"].Value;
    string cmdday = xmlNode.Attributes["cmdday"].Value;
    string cmdmonth = xmlNode.Attributes["cmdmonth"].Value;
    string cmdyear = xmlNode.Attributes["cmdyear"].Value;
    string cmdh = xmlNode.Attributes["cmdh"].Value;
    string cmdm = xmlNode.Attributes["cmdm"].Value;
    string cmdetat = xmlNode.Attributes["cmdetat"].Value;
    string payer = xmlNode.Attributes["payer"].Value;
    string payeday = xmlNode.Attributes["payeday"].Value;
    string payemonth = xmlNode.Attributes["payemonth"].Value;
    string payeyear = xmlNode.Attributes["payeyear"].Value;
    string payeh = xmlNode.Attributes["payeh"].Value;
    string payem = xmlNode.Attributes["payem"].Value;
    string payemethod = xmlNode.Attributes["payemethod"].Value;

    if (cmd != '')
    return true;
    }

    return false;
    }
    }
    }

    Can I return an array ?

    Thanks
    Sebastien
     
  2. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,080
    Likes Received:
    77
    Hello Sebastien,

    You can return the type you want. You can create a class/struct with all your variables, and return that object. Or you can return an array of objects. That's up to you. You just need to edit the method signature, from:
    public bool ValidateXML(string fileContent)

    to:
    public MyStruct ValidateXML(string fileContent)

    or
    public object[] ValidateXML(string fileContent)

    Then, depending on the value you return, you will need different expressions to use the value returned.

    Kind regards.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. SEBASTIEN CARROUGET

    Joined:
    Jan 31, 2018
    Messages:
    2
    Likes Received:
    0
    Ok, thanks for the information, but I do not really know how to do it.

    I need to store all the xml datas in variables that can be used in differents components of the callflow.
    Becasue I want to used thoses variables in speech to text to play prompt to the caller the status of his order (order number, order date, payment date and method...) :

    XML file content :
    <appel client="47596" cmd="53646" cmdday="20" cmdmonth="febrary" cmdyear="2018" cmdh="20" cmdm="11" payer="1" payeday="20" payemonth="febrary" payeyear="2018" payeh="20" payem="14" payemethod="CB" payeconf="SINGLE" cmdindic="CMD in BE" cmdstat="5"/>

    CFD variables :
    callflow$.cmdid = cmd
    callflow$.cdmdate = cmdday + cmdmonth + cmdyear
    ...Etc

    I d'ont know how to construct the signature or objet aray and how to create variables to use in differents components of my call flow.

    Could you help me please ?
    Could you tell me more about the array construction and how to return the array to CFD and associate variables with data array ?

    Sebastien
     
  4. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,080
    Likes Received:
    77
    Hello Sebastien,

    First, you can use the following C# code for XML validation and returning the struct:
    Code:
    using System.Xml;
    namespace TextFileValidator
    {
        public struct ValidationResult
        {
            public bool success = false;
            public string cmd;
            public string cmdday;
            public string cmdmonth;
            public string cmdyear;
            public string cmdh;
            public string cmdm;
            public string cmdetat;
            public string payer;
            public string payeday;
            public string payemonth;
            public string payeyear;
            public string payeh;
            public string payem;
            public string payemethod;
        }
       
        public class Validator
        {
            public ValidationResult ValidateXML(string fileContent)
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(fileContent);
                XmlNodeList customerElementList = xmlDocument.GetElementsByTagName("appel");
                foreach (XmlNode xmlNode in customerElementList)
                {
                    ValidationResult result = new ValidationResult();
                    result.cmd = xmlNode.Attributes["cmd"].Value;
                    result.cmdday = xmlNode.Attributes["cmdday"].Value;
                    result.cmdmonth = xmlNode.Attributes["cmdmonth"].Value;
                    result.cmdyear = xmlNode.Attributes["cmdyear"].Value;
                    result.cmdh = xmlNode.Attributes["cmdh"].Value;
                    result.cmdm = xmlNode.Attributes["cmdm"].Value;
                    result.cmdetat = xmlNode.Attributes["cmdetat"].Value;
                    result.payer = xmlNode.Attributes["payer"].Value;
                    result.payeday = xmlNode.Attributes["payeday"].Value;
                    result.payemonth = xmlNode.Attributes["payemonth"].Value;
                    result.payeyear = xmlNode.Attributes["payeyear"].Value;
                    result.payeh = xmlNode.Attributes["payeh"].Value;
                    result.payem = xmlNode.Attributes["payem"].Value;
                    result.payemethod = xmlNode.Attributes["payemethod"].Value;
                   
                    if (result.cmd != "")
                    {
                        result.success = true;
                        return result;
                    }
                }
                return new ValidationResult();
            }
           
            public string GetCmd(ValidationResult result)
            {
                return result.cmd;
            }
           
            public string GetCmdday(ValidationResult result)
            {
                return result.cmdday;
            }
           
            // Continue adding the getter methods...
        }
    }
    
    
    Please note that you need methods to get each part of the struct, I just added 2 but you need to add all of them.

    Finally, you invoke these methods to get each part of the struct, using a Launch External Script component. You need to pass the result of the first Launch External Script component, which holds the ValidationResult object.

    Hope it helps.

    Kind regards.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Thread Status:
Not open for further replies.