1
votes

xml as below :

<Item>
   <Winner>2</Winner>
</Item>

For my class definition I have the following:


    public enum HomeOrAwayTeamType {Home =1,Away =2,Draw =3,NA = 0};

    class Item
    {
        [XmlIgnore]
        public virtual HomeOrAwayTeamType Winner { get; set; }

        [XmlElement(ElementName = "Winner"), XmlText]
        public virtual string WinnerSerializer
        {
            get { return this.Winner.ToString(); }
            set
            {
                //get 'Away' from HomeOrAwayTeamType
                this.Winner = (HomeOrAwayTeamType)2; //ok
                this.Winner = (HomeOrAwayTeamType)Convert.ToInt32("2"); //ok
                this.Winner = (HomeOrAwayTeamType)int.parse("2"); //ok 

                //get 'NA' from HomeOrAwayTeamType
                this.Winner = (HomeOrAwayTeamType)Convert.ToInt32(value); //fail
                this.Winner = (HomeOrAwayTeamType)int.parse(value); //fail
             }
         }    
    }

    string xml = ""; //xml code
    Item model = default(Item);

    using (var reader = XmlReader.Create(new StringReader(xml)))
    {
        var serializer = new XmlSerializer(typeof(Item));
        model = (Item)serializer.Deserialize(reader);
    }

hi, guys.. How get int value from XmlTextAttribute?

Please help~

1

1 Answers

0
votes

Your xml string is empty. This works for me:

string xml = "<Item><Winner>2</Winner></Item>"; //xml code

this.Winner gets set to Away