Prometheus
Prometheus

Reputation: 1251

How to obtain Enum value from stringified XML?

I have a method that gets as parameter a stringified XML and returns an object with specific fields of the string as its properties.The fields are mapped with class fields.

So far i can get the result (processedObj) with two properties(Id,Title) but not the Enum property . How can i make it recognizable and obtain its current value ?

Main class

email.Content = '<EmailClass><ID>210</ID><Title>Urgent</Title><Date>2020-06-01</Date> 
              <Action>Delete</Action></EmailClass>'

Dim processedObj = XmlProcessor.ObjectfromStrXml(Of EmailClass)(email.Content)

EmailClass class

<Serializable()>
<XmlRoot("EmailClass")>
Public Class EmailClass

    Public ID As String
    Public Title As String
 
    Public Enum Action
        Send
        Delete
    End Enum

End Class

XmlProcessor class

public static T ObjectfromStrXml<T>(string p_StrXML){
            T result;
            using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(p_StrXML)))
            using (StreamReader stReader = new StreamReader(mStream , Encoding.UTF8))
            using (XmlReader reader = new XmlTextReader(stReader))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                result = (T)(serializer.Deserialize(reader));
                streamReader.Close();
                memoryStream.Close();
                reader.Close();
            }

            return result;
}

Upvotes: 0

Views: 92

Answers (1)

jdweng
jdweng

Reputation: 34421

Try code below :

Public Class EmailClass

    Public ID As String
    Public Title As String
    Private PAction As EAction

    Public Property Action As String
        Get
            Return PAction.ToString()
        End Get
        Set(value As String)
            PAction = [Enum].Parse(GetType(EAction), value)
        End Set
    End Property


    Public Enum EAction
        Send
        Delete
    End Enum

End Class

Upvotes: 1

Related Questions