0

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.

ic3man7019
  • 721
  • 6
  • 24
  • The class Transmission should be in global space so all the forms have access to the structure I would also add a Static variable to the class so all forms have access to the instance. Something like : public static Transmission transmission { get; set; }. – jdweng Oct 26 '17 at 13:56
  • What exactly is your problem? Is it that 1) you have designed some classes and serialization to XML fails? If so, please share a [mcve] and the full `ToString()` output of the exception. Or is it 2) You have some predefined XML structure and you need to design c# classes to match it? If the latter we will need to see sample XML for which you need help. – dbc Oct 26 '17 at 18:24
  • Or is your question just 3) *How to serialize to XML in c#*? If so there are already many similar questions e.g. [Serialize an object to XML](https://stackoverflow.com/q/4123590) or [How to serialize/deserialize simple classes to XML and back](https://stackoverflow.com/q/3356976). Have you checked any of these? – dbc Oct 26 '17 at 18:24

1 Answers1

0

If you are trying to create a XML file from the serialized form data at server side then following solution may work.

Based on a predetermined XML structure arrange properties in your ViewModel. Post the form to server normally. Then convert posted C# object model to XML at server side.

You can refer following link to write C# Object to an XML file

Write Object Data to an XML File (C#)

Tanmay
  • 1,123
  • 1
  • 10
  • 20
  • 1
    As he's saying, you can serialize a serialize-able class. So just create a class on the server side that has the format you want, and then serialize that. Which seems like where the original question was already heading towards, as the model appears to contain what you wanted, more or less from the code that you did provide. https://stackoverflow.com/questions/5877808/what-is-serializable-and-when-should-i-use-it – Greg Oct 26 '17 at 14:00
  • @Greg Thanks for the pointer. I think I already have what I need in terms of the models and everything. I'm more interested in knowing how to actually serialize the data and create a file, though. I'm trying to decide whether to do it on the client side or server side. My inclination is to do it on the server side, but I'm not sure... – ic3man7019 Oct 26 '17 at 14:18
  • Check out that link I provided. I had a feeling you wanted to dig deeper, and there should be examples related in there on how to proceed. The last solution has code. – Greg Oct 26 '17 at 14:20
  • Another example https://stackoverflow.com/questions/2434534/serialize-an-object-to-string – Greg Oct 26 '17 at 14:22
  • @Greg Thanks a lot for the links. I'm going through them now. Also, thanks to Tanmay for the link. I'm looking over everything. – ic3man7019 Oct 26 '17 at 14:32