Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 466.4k

Deserialization of data using xml in asp.net mvc

Jun 5 2023 7:37 PM

Hi Team

I have xml data structure and method to read this structure, but i often get an exception of deserilization that is not correct and need some help. 

 

    <SalesOrder>
        <OrderHeader>
            <OrderNumber>SO625144</OrderNumber>
            <OrderType>Normal</OrderType>
            <OrderStatus>New</OrderStatus>
            <CustomerName>KFC</CustomerName>
            <CreateDate>2018-03-08 13:52:21</CreateDate>
        </OrderHeader>
        <OrderLine>
            <LineNumber>1</LineNumber>
            <ProductCode>GSX837420</ProductCode>
            <ProductType>Parts</ProductType>
            <CostPrice>13.54</CostPrice>
            <SalesPrice>84.49</SalesPrice>
            <Quantity>10</Quantity>
        </OrderLine>
        <OrderLine>
            <LineNumber>2</LineNumber>
            <ProductCode>AVF697420</ProductCode>
            <ProductType>Apparel</ProductType>
            <CostPrice>19.34</CostPrice>
            <SalesPrice>105.99</SalesPrice>
            <Quantity>25</Quantity>
        </OrderLine>
    </SalesOrder>
    <SalesOrder>
        <OrderHeader>
            <OrderNumber>SO625145</OrderNumber>
            <OrderType>Normal</OrderType>
            <OrderStatus>New</OrderStatus>
            <CustomerName>Nandos</CustomerName>
            <CreateDate>2018-03-09 08:56:21</CreateDate>
        </OrderHeader>
        <OrderLine>
            <LineNumber>1</LineNumber>
        </OrderLine>
    </SalesOrder>

// deserializing xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Hosting;
using OrderApplicationXML.Models;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace OrderApplicationXML.Data_Access_Layer
{
    public class OrderXmlDataAccess
    {
        // declaring some objects for memory allocations.
        private static string FilePath => @"C:\Users\Zux\source\repos\OrderApplicationXML\OrderApplicationXML\OrderLine.xml";



        // constructor.
        public static List<SalesOrder> GetAllOrders()
        {
            var serializer = new XmlSerializer(typeof(List<SalesOrder>));
            using (var stream = new FileStream(FilePath, FileMode.Open))
            {
                var xmlReaderSettings = new XmlReaderSettings()
                {
                    CloseInput = true,
                    IgnoreWhitespace = true,
                    IgnoreComments = true
                };

                using (XmlReader xmlReader = XmlReader.Create(stream, xmlReaderSettings))
                {
                    return (List<SalesOrder>)serializer.Deserialize(xmlReader);
                }
            }
        }


        // get order.
        public static SalesOrder GetOrderById(string orderNumber)
        {
            var orders = GetAllOrders();
            return orders.FirstOrDefault(o => o.OrderNumber == orderNumber);
        }

        // add an order.
        public static void AddOrder(SalesOrder order)
        {
            var orders = GetAllOrders();
            orders.Add(order);
            SaveOrders(orders);
        }

        // save orders.
        private static void SaveOrders(List<SalesOrder> orders)
        {
            var serializer = new XmlSerializer(typeof(List<SalesOrder>));
            using(var stream = new FileStream(FilePath, FileMode.Create))
            {
                serializer.Serialize(stream, orders);
            }
        }

        //update an existing order.
        public static void UpdateOrder(SalesOrder order)
        {
            var orders = GetAllOrders();
            var existingOrder = orders.FirstOrDefault(o => o.OrderNumber == order.OrderNumber);
            if(existingOrder != null)
            {
                orders.Remove(existingOrder);
                orders.Add(order);
                SaveOrders(orders);
            }
        }

        // delete an order.
        public static void DeleteOrder(string orderNumber)
        {
            var orders = GetAllOrders();
            var existingOrder = orders.FirstOrDefault(o => o.OrderNumber == orderNumber);

            if(existingOrder !=null)
            {
                orders.Remove(existingOrder);
                SaveOrders(orders);
            }
        }
    }
}

// controller
public ActionResult Delete(string id)
        {
            var order = OrderXmlDataAccess.GetOrderById(id);
            if(order == null)
            {
                
            }
            
            return View(order);
        }

// model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OrderApplicationXML.Models
{
    public class SalesOrder
    {
        public OrderHeader OrderHeader { get; set; }
        public List<OrderLine> OrderLines { get; set; }

        public string OrderNumber { get; set; }
    }

    
    
}

 


Answers (2)