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.

4

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

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.