How to deserialize attribute named “value” with RestSharp without breaking the code style?

StackOverflow https://stackoverflow.com/questions/21493040

  •  05-10-2022
  •  | 
  •  

Question

I am using RestSharp to deserialize a XML file where some of the nodes are like this:

<clouds value="68" name="broken clouds"/>

The elementes with an attribute called 'value' will not deserialize.

My class:

public class CloudsData
{        
    public string value { get; set; }

    public string Name { get; set; } 

}

Renaming "Value" to "value" helps, but breaking the Code Style. Are there other ways to solve this problem?

Était-ce utile?

La solution

Mark your property with SerializeAsAttribute:

public class CloudsData
{        
    [SerializeAs(Name = "value")]
    public string value { get; set; }

    [SerializeAs(Name = "name")]
    public string Name { get; set; } 
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top