0

I have an ASP.NET web application that basically consists of a form with many fields that have to be serialized to XML. There is a predetermined structure that the server expects; I have set up my classes (models) to reflect this structure. I have decorated the model classes with the [Serializable] attribute so that the XML structure is preserved. I have tested the serialization; it is working properly. Here is how I tested it:

[HttpPost]
        public XmlResult Sample2(Transmission t)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    XmlResult xrs = new XmlResult(t);
                    xrs.ExecuteResult(ControllerContext);
                    return xrs;
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

Here is the XmlResult class I'm using:

public class XmlResult : ActionResult
    {
        private object objectToSerialize;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlResult"/> class.
        /// </summary>
        /// <param name="objectToSerialize">The object to serialize to XML.</param>
        public XmlResult(object objectToSerialize)
        {
            this.objectToSerialize = objectToSerialize;
        }

        /// <summary>
        /// Gets the object to be serialized to XML.
        /// </summary>
        public object ObjectToSerialize
        {
            get { return objectToSerialize; }
        }

        /// <summary>
        /// Serializes the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
        /// </summary>
        /// <param name="context">The controller context for the current request.</param>
        public override void ExecuteResult(ControllerContext context)
        {
            if (objectToSerialize != null)
            {
                context.HttpContext.Response.Clear();
                var xs = new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
                context.HttpContext.Response.ContentType = "xml";
                xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
            }
        }
    }

Now that I know the data is being serialized in the correct XML format, how can I POST this data to the server using jQuery? Note that I am sending the XML data to a customer's URL via a HTTPPOST request.

ic3man7019
  • 721
  • 6
  • 24
  • Is there a reason you want to serialize it before it goes to the server? You could always post your model and serialize the form server side. However I think you could probably just serialize the form as JSON first and then post that server side – Master Yoda Nov 14 '17 at 14:45
  • @MasterYoda Thanks for the input. I suppose I was just thinking of making sure the XML was structured correctly. The server will throw an error if the XML is not structured correctly. I admit that I don't have much experience doing this kind of thing in ASP.NET MVC. – ic3man7019 Nov 14 '17 at 14:50
  • 1
    I would set the server up to accept a jsonresult instead of xmlresult, unless you need to use xml for any reason? check out the docs here: https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx After that you can just pass the form in JSON format to the server and let the server do its thing – Master Yoda Nov 14 '17 at 14:51
  • @MasterYoda I suppose I should've added that I'm sending the data to a customer's URL via a POST request. They expect XML; that cannot be changed. – ic3man7019 Nov 14 '17 at 14:52
  • 1
    Fair enough, then you are correct in that you will have to serialize the data as XML server side. You can parse JSON as XML when it arrives server side: https://stackoverflow.com/questions/814001/how-to-convert-json-to-xml-or-xml-to-json Unfortunately MVC can be quite strict about serializing complex objects such as parent classes that contain multiple child classes/properties so parsing as JSON should be quite beneficial – Master Yoda Nov 14 '17 at 14:54
  • @MasterYoda Thanks for the links. I'm looking through them now. I'm fairly new to ASP.NET MVC. I don't suppose you know of any links that explain the workflow a little better, do you? I'm not exactly sure what I should be doing where; for instance, I am returning the `XmlResult` in my controller action now, which is showing me that the XML is data is in the correct format. I'm still not 100% sure what comes next as far as changing the code to POST the data to the remote URL. I hope that makes sense. – ic3man7019 Nov 14 '17 at 15:00
  • 1
    Sure, heres a great answer/tutorial to that question on SO: https://stackoverflow.com/questions/33947882/pass-model-to-controller-using-jquery-ajax Check out his section on binding complex classes also. Here is another good tutorial you can check out: http://www.c-sharpcorner.com/UploadFile/302f8f/Asp-Net-mvc-using-jquery-ajax/. Give me a shout if you need anything else, im aware that we will need to move this discussion to chat soon – Master Yoda Nov 14 '17 at 15:05
  • @MasterYoda Thank you. I'm looking it over now. – ic3man7019 Nov 14 '17 at 15:09
  • @MasterYoda If you're available to chat, I'd like to bounce my idea off of you to get some feedback so that I understand the links you gave me more thoroughly. If not, thanks anyway; you've been very helpful. – ic3man7019 Nov 14 '17 at 15:40
  • Hey, im currently working on something at the moment but ill be available around 17.00 (gmt) for 20 minutes or so. – Master Yoda Nov 14 '17 at 15:43
  • @MasterYoda Sounds great. Thanks. – ic3man7019 Nov 14 '17 at 15:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158984/discussion-between-master-yoda-and-ic3man7019). – Master Yoda Nov 14 '17 at 16:55

0 Answers0