-1

Problem: How to parse enum string & value from XML model to object.

Error: Cannot parse enum value from XML to object model.

I tried to parse only enum value, but cannot parse to model. I tried some solutions but I still faced issue, please help to advise! Thanks for your support!

  • Enum:

    public enum DocumentType
    {
        DocumentType_A = 0,
        DocumentType_B = 1,
        DocumentType_C = 2
    }
    
  • XML:

    Image

Munim Munna
  • 17,178
  • 6
  • 29
  • 58
Huu Ha
  • 3
  • 4
  • 1
    Post the XML as _text_, post the code you used to deserialize. Post the target C# class. – bommelding Jun 13 '18 at 13:07
  • i guess that the number is from something like an external API? you could work around that using a separated property of the type `int` and some `XmlIgnore` property that just reads/writes to that field essentially parsing by hand. – X39 Jun 13 '18 at 13:07
  • @X39 I want to read value from XML file. How to use XmlIgnore to read value in this case? Please share some example. Thank you! – Huu Ha Jun 13 '18 at 13:11
  • 1
    @HuuHa would be nice to know what exactly you are doing, are you just reading the document by hand or are you serializing it into some object? provide some minimum-viable-example and the correct answer should be fairly close – X39 Jun 13 '18 at 13:17
  • How to parse document type in this deserialize method below: Document overview = (Document)reader.Deserialize(file); public class Document { public long DocumentId { get; set; } public DocumentType Type{ get; set; } } – Huu Ha Jun 13 '18 at 13:22
  • Not very readable, [edit] your question. – bommelding Jun 13 '18 at 13:23
  • @X39: See my comment. – Huu Ha Jun 13 '18 at 13:30
  • @HuuHa it still is not really very clear what you are doing. Could you edit the original question to at least match the code you use? also check out this as it might be your solution: https://stackoverflow.com/questions/506368/how-do-i-serialize-an-enum-value-as-an-int – X39 Jun 13 '18 at 13:33
  • @X39 That's great! It worked for me. Thank you for your help! – Huu Ha Jun 13 '18 at 13:49

1 Answers1

1

First get the node from the XML and then parse it to an enum.

const string str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Document><DocumentId>1</DocumentId><Attachment></Attachment><Metadata><DocumentType>0</DocumentType></Metadata></Document>";
var xml = new XmlDocument();
xml.LoadXml(str);

var node = xml.DocumentElement.SelectSingleNode("/Document/Metadata/DocumentType").InnerText;
DocumentType currentEnum = (DocumentType)Enum.Parse(typeof(DocumentType), node);
int currentEnumId = (int)currentEnum;

UPDATE based on comments - Deserialize xml to object with enums

XML

<?xml version="1.0" encoding="utf-8"?>
<Document>
<DocumentId>1</DocumentId>
<Attachment></Attachment>
<Metadata>
    <DocumentType>1</DocumentType>
</Metadata>
</Document>

Models

    public class Document 
    { 
        public long DocumentId { get; set; } 

        public Metadata Metadata { get; set; } 
    }

    public class Metadata
    {

        public string DocumentType  { get; set; } 

        public DocumentType Type 
        {
            get
            {
                return (DocumentType)Enum.Parse(typeof(DocumentType), DocumentType);
            }
        }

    }

    public enum DocumentType 
    { 
        DocumentType_A = 0, 
        DocumentType_B = 1, 
        DocumentType_C = 2 
    };

Converting XML to Object:

    var serializer = new XmlSerializer(typeof(Document)); 
    using (var reader = XmlReader.Create("/Users/brunomartinspro/Desktop/myxml.xml")) 
    { 
        Document document = (Document)serializer.Deserialize(reader); 
        var mySuperType = document.Metadata.Type; 
    }
BrunoMartinsPro
  • 1,646
  • 1
  • 24
  • 48