Skip to content

Instantly share code, notes, and snippets.

@brunomartinspro
Last active June 13, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunomartinspro/acf53de44d03736b270107240e278da4 to your computer and use it in GitHub Desktop.
Save brunomartinspro/acf53de44d03736b270107240e278da4 to your computer and use it in GitHub Desktop.
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