Rajanikanth Ragula
Rajanikanth Ragula

Reputation: 53

Deserialize different types of Elements into one list of XmlElement in C#

I want to Deserialize two different kind of Xml elements into one List as I want to preserve the order

<Step name="Login to Account" >
  <Action name="LoginToWebsite" type="Login" />
  <NewAction name="EnterTextInTextBox" type="SendKeysToTextBox" extraAttribute="Testvalue" />
</Step>

currently I am using the following to get the two actions into one list but I have to change the element name to NewAction (like above) instead of Action for the second one

[XmlRoot(ElementName = "Step")]
public class WorkflowStep
{    
  [XmlAnyElement("Action")]
    public XmlElement[] Actions
    {
        get;
        set;
    }
}

As the XmlAnyElement is bound to "Action" Element name, how can I change to support two different Element Names but need to be Deserialized into one array

Upvotes: 0

Views: 287

Answers (1)

user6996876
user6996876

Reputation:

What you want is

[XmlRoot(ElementName = "Step")]
public class WorkflowStep
{
    [XmlAnyElement("Action")]
    [XmlAnyElement("NewAction")]
    public XmlElement[] Actions
    {
        get;
        set;
    }
}

Upvotes: 1

Related Questions