Last active
June 13, 2018 15:33
-
-
Save brunomartinspro/acf53de44d03736b270107240e278da4 to your computer and use it in GitHub Desktop.
Convert property to a enum from a XML Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BrunoMartinsPro | |
{ | |
public class DocumentModel | |
{ | |
public long DocumentId { get; set; } | |
public MetadataModel Metadata { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BrunoMartinsPro | |
{ | |
public enum DocumentTypeEnum | |
{ | |
Word = 0, | |
Excel = 1, | |
PDF = 2 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BrunoMartinsPro | |
{ | |
public class MetadataModel | |
{ | |
public string DocumentType { get; set; } | |
public DocumentTypeEnum Type | |
{ | |
get | |
{ | |
return (DocumentTypeEnum)Enum.Parse(typeof(DocumentTypeEnum), DocumentType); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Document> | |
<DocumentId>1</DocumentId> | |
<Metadata> | |
<DocumentType>1</DocumentType> | |
</Metadata> | |
</Document> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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