Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert property to a enum from a XML Object
namespace BrunoMartinsPro
{
public class DocumentModel
{
public long DocumentId { get; set; }
public MetadataModel Metadata { get; set; }
}
}
namespace BrunoMartinsPro
{
public enum DocumentTypeEnum
{
Word = 0,
Excel = 1,
PDF = 2
}
}
namespace BrunoMartinsPro
{
public class MetadataModel
{
public string DocumentType { get; set; }
public DocumentTypeEnum Type
{
get
{
return (DocumentTypeEnum)Enum.Parse(typeof(DocumentTypeEnum), DocumentType);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Document>
<DocumentId>1</DocumentId>
<Metadata>
<DocumentType>1</DocumentType>
</Metadata>
</Document>
using System;
using System.Xml;
using System.Xml.Serialization;
namespace BrunoMartinsPro
{
class Program
{
static void Main(string[] args)
{
//Serialize Document
var serializer = new XmlSerializer(typeof(DocumentModel));
//Read XML File
using (var reader = XmlReader.Create("/Users/brunomartinspro/Desktop/myxml.xml"))
{
//Deserialize Object
DocumentModel document = (DocumentModel)serializer.Deserialize(reader);
//Get Enum Type
var devilTrigger = document.Metadata.Type;
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment