0

I'm very new to XML. The following xml is received as a string from a webservice

"<settings>   
  <calculator display="1" />
  <details display="1" />
  <charge display="1" />
  <features>
    <feature code="HAZ" description="CARGO" />
    <feature code="IDL" description="DELIVERY" />
    <feature code="LFT" description="TRUCK" />
    <feature code="NFY" description="CARRIER CHARGE" />
  </addons>
</settings>  "

And below are user-configuration which has a list as a property.

 public class UserConfiguration
 {
     public int calculator { get; set; }

     public int details { get; set; }

     public int charge { get; set; }

     public List<Accessorial> features { get; set; }
 }

 public class Accessorial
 {
     public string code { get; set; }

     public string description { get; set; }
 }

I have tried the following but the values are null;

XmlSerializer deserializer = new XmlSerializer(typeof(UserConfiguration), new XmlRootAttribute("root"));
var objectValue = deserializer.Deserialize(new StringReader(xml));

I had also put XmElement("calculator") and so, on the properties according to some answers on stackoverflow but they also didn't work.

adiga
  • 34,372
  • 9
  • 61
  • 83

1 Answers1

5

Use below Contract with Attribute Programming:

[XmlRoot("settings")]
public class Settings
{
   [XmlElement("calculator")]
   public Calculator calculator { get; set; }

   [XmlArray("features")]
   [XmlArrayItem("feature")]
   public List<Feature> features {get; set; }
}

public class Calculator 
{
    [XmlAttribute]
    public string display { get; set; }
}
adiga
  • 34,372
  • 9
  • 61
  • 83
tom
  • 719
  • 1
  • 11
  • 30