0

I have a c# MVC model which I would like to serialize to an XML document to look like this:

<Vehicle>
    <Type color="red" speed="50mph">Ford</Type>
    <Type color="blue" speed="70mph">Toyota</Type>
</Vehicle>

Here is the model:

[Serializable]
public class Production
{
    public List<Vehicle> Vehicles { get; set; }
}

[Serializable]
public class Vehicle
{
    [XmlAttribute]
    public string color { get; set; }

    [XmlAttribute]
    public string speed { get; set; }
}

Since class Vehicle is not a property what do I need to add to the class to give it a value of for example "ford" or "Toyota"

Right now I have:

var myvehicle = new Vehicle {color = "red", speed = "50mph"};
1
  • From the code you have, you are not storing the make anywhere...where does the data come from?
    – lc.
    Commented Aug 27, 2013 at 1:44

1 Answer 1

1

Add another property with the [XmlText] attribute:

[XmlText]
public string Make {get; set;}

You should also add attributes to your list of Vehicles:

[XmlArray("Vehicle")]
[XmlArrayItem("Type")]
public List<Vehicle> Vehicles { get; set; }

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.