0

I have enum like this, i am getting value from datavase like "CHA-MD-1"

// this is generated by visual studio command promt
public enum CHATYPE{
      
    [System.Xml.Serialization.XmlEnumAttribute("CHA-MD-1")]
    CHAMD1,
       
    [System.Xml.Serialization.XmlEnumAttribute("CHA_MD-2")]
    CHAMD2,
}

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public CHATYPE chatype{
        get {
            return this.chatype;
        }
        set {
            this.chatype= value;
        }
    }

data.chatype = "CHA-MD-1"(getting from database) -- here iam getting exception, please help me out in this?? , i want to generate xml with CHA-MD-1 value data.chatype = (CHATYPE)Enum.Parse(typeof(CHATYPE),"CHA-MD-1"); //expection

DerStarkeBaer
  • 669
  • 8
  • 28
PR13 R
  • 25
  • 1
  • 7
  • Does this answer your question? [Get element of an enum by sending XmlEnumAttribute c#?](https://stackoverflow.com/questions/42990069/get-element-of-an-enum-by-sending-xmlenumattribute-c) – dymanoid Jul 10 '20 at 12:09
  • Yuo can use `(CHATYPE )enum.Parse(typeof(CHATYPE ), "CHA-MD-1")` to convert text to enum value. – Michael Jul 10 '20 at 12:27
  • @Michael i have use that only there only i am getting exception – PR13 R Jul 10 '20 at 12:30
  • data.chatype = (CHATYPE)Enum.Parse(typeof(CHATYPE),"CHA-MD-1"); //expection – PR13 R Jul 10 '20 at 12:30
  • I want to generate with CHA-MD-1 with this string – PR13 R Jul 10 '20 at 12:46
  • 1
    @PR13R ahh, of course the enum values and database text are not the same. In this case the easist way to do this is by converting this manually with a switch. If you do want a generic way and you don't want to rename the enum values, you need to use reflection to read the `XmlEnumAttribute` to get the database name. See more here: https://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value – Michael Jul 10 '20 at 12:46

0 Answers0