0

Using Axis Communications VAPIX WSDL APIs - I'm setting up a NewActionConfiguration which takes a number of parameters that I have saved in a List but the way the API documents have the implementation I cant loop through my parameter list XML objects while defining the newAction object.

//This is how the API docs say to do it:
NewActionConfiguration newAction = new NewActionConfiguration
{
    TemplateToken = overlayToken,
    Name = "Overlay Text",
    Parameters = new ActionParameters
    {
        Parameter = new[]
        {
            new ActionParameter { Name = "text", Value = "Trigger:Active" },
            new ActionParameter { Name = "channels", Value = "1" },
            new ActionParameter { Name = "duration", Value = "15" }
         }
     }
};

//This is what I need to do:
 NewActionConfiguration newAction = new NewActionConfiguration
 {
     Name = xmlPrimaryAction["Name"].InnerText,
     TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
     Parameters = new[]
     {
         foreach (ActionParameter actionParameter in actionParameterList)
         {
             new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value };
          }
      }
};

The API will not allow me to just do a: newAction.Parameters.Parameter.Add(actionParameter) or the like. Anyone got any ideas?

Dillon Tucker
  • 55
  • 1
  • 8

2 Answers2

0

So first things, you cannot declare a foreach block inside an object instantiating scope block. What you need to do is declare a variable before, in the function scope and then attribute the Parameter property to it. Like so:

var actionParameters = new List<ActionParameter>();
foreach (ActionParameter actionParameter in actionParameterList)
     {
         actionParameters.Add(new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value });
      }
NewActionConfiguration newAction = new NewActionConfiguration
 {
     Name = xmlPrimaryAction["Name"].InnerText,
     TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
     Parameters = actionParameters.ToArray()//Use System.Linq here to convert the list into an array
};
0

Found it! Thanks for the help @Vitor you were close but learned how to cast my list as my object after i found this: Convert List to object[]

Here's what finally worked:

var actionParameterList = new List<ActionParameter>();
foreach (XmlNode xmlParameter in xmlActionParameters)
{
    ActionParameter actionParameter = new ActionParameter();
    actionParameter.Name = xmlParameter["Name"].InnerText;
    actionParameter.Value = xmlParameter["Value"].InnerText;
    actionParameterList.Add(new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value });
}
 NewActionConfiguration newAction = new NewActionConfiguration
 {
     Name = xmlPrimaryAction["Name"].InnerText,
     TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
     Parameters = new ActionParameters
     {
         Parameter = actionParameterList.Cast<ActionParameter>().ToArray()
     }
 };
Dillon Tucker
  • 55
  • 1
  • 8