Search code examples
c#arraysxmlforeachapache-axis

Need to add Parameters to NewActionConfiguration but cant use a foreach inside the definition


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?


Solution

  • 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()
         }
     };