1

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~

2

1 Answer 1

0

Your xml string is empty. This works for me:

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

this.Winner gets set to Away

Sign up to request clarification or add additional context in comments.

6 Comments

thanks.. but yet not gets 'Away' value from enumType
What do you get? Do you get an exception? What do you mean when you say fail over here this.Winner = (HomeOrAwayTeamType)int.parse(value); //fail
for example , in xml , if TextNode value of "Winner" Element is 2 (<Winner>2</Winner>) then i hope to get "Away" of enumType. but, i get "NA" of enumType when execute this.Winner = (HomeOrAwayTeamType)int.parse(value); how get correct value of enumType Corresponding to xml value.
sorry. i'm english beginner. because not enoughmy express.
i solved this problem. my mistake..haha it is to remove 'TextXml' thanks a lot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.