I have a View in my ASP.NET MVC web application that contains a single form. The form has fields from several different models, so I'm using a ViewModel (of sorts) that contains all of the different models necessary. I need to be able to serialize the data entered into the form to XML, based on a predetermined XML structure. I'm not including the code for my models because there are just public properties in each of them, as well as the "navigation" properties to the other models that the model may contain (there is an example of this further below). Here are my models:
Cargo.cs
CargoDetails.cs
Consignee.cs
Request.cs
Shipper.cs
Transmission.cs
TransmissionHeader.cs
Here is the desired XML structure:
<Transmission>
<TransmissionHeader>
.
.
</TransmissionHeader>
*<Request>
.
.
*<Cargo>
<Shipper>
.
.
</Shipper>
<Consignee>
.
.
</Consignee>
*<CargoDetails>
.
.
</CargoDetails>
</Cargo>
</Request>
</Transmission>
The * denotes that there may be more than one of these elements.
I have omitted the elements inside the "root" elements for brevity, but these are obviously the fields in the form, which are also the properties in the models.
I am using Transmission.cs
as my "ViewModel" because, as you can see in the XML structure, it contains all of the other elements. Here is the code for the Transmission
model:
[Serializable]
public class Transmission
{
public TransmissionHeader TransmissionHeader { get; set; }
public List<Request> Requests { get; set; }
}
How can I go about serializing the data entered into the form to XML, based on this XML structure? I would like to create a XML file from the serialized form data.
If I can further explain anything, please let me know. Also, if any part of this is set up poorly, let me know. This is my first attempt at doing something like this in ASP.NET MVC. Thanks.