Topic:   How to Generate and Download XML Report from Controller in ASP.Net MVC
Nov 05, 2020 05:12 2 Replies 32 Views ARUNA

Orders

OrderId  OrderName  

1               a

2                b

Items

OrderId  ItemId  ItemName 

1               1              aa

1                2              bb

2                  3             cc

Taxes

ItemId   TaxId    TaxName

1             1            ff

1              2             gg

2               3             hh

Currently my requirement is that based upon OrderId generate XML report and make it downloadable and save it folder

Can you please help me 

just create one method and then generate xml report

1

2

3

4

public void GenerateXmlReport()

{

    int oId = 1;

}

Prev Next
Topic Replies (2)
  1. 1
    idnkx user

    DIVYA

    Controller
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81 public class HomeController : Controller
    {
    // GET: Home
    public ActionResult Index()
    {
    return View();
    }

    public ActionResult GenerateXmlReport(string txtOrderId)
    {
    byte[] bytes = new byte[] { };
    XElement xml = new XElement("Orders",
    from o in GetOrders()
    join i in GetItems() on o.OrderId equals i.OrderId
    join t in GetTaxes() on i.ItemId equals t.ItemId
    where o.OrderId == Convert.ToInt32(txtOrderId)
    select new XElement("Order",
    new XElement("OrderId", o.OrderId),
    new XElement("OrderName", o.OrderName),
    new XElement("ItemId", i.ItemId),
    new XElement("ItemName", i.ItemName),
    new XElement("ItemId", t.ItemId),
    new XElement("TaxName", t.TaxName))
    );
    if (!Directory.Exists(Server.MapPath("~/Files/")))
    {
    Directory.CreateDirectory(Server.MapPath("~/Files/"));
    }
    xml.Save(Server.MapPath("~/Files/") + "Order.xml");
    return File(Server.MapPath("~/Files/") + "Order.xml", "text/xml", "Order.xml");
    }

    private List GetOrders()
    {
    List orders = new List();
    orders.Add(new Order { OrderId = 1, OrderName = "a" });
    orders.Add(new Order { OrderId = 2, OrderName = "b" });

    return orders;
    }

    private List GetItems()
    {
    List items = new List();
    items.Add(new Item { OrderId = 1, ItemId = 1, ItemName = "aa" });
    items.Add(new Item { OrderId = 1, ItemId = 2, ItemName = "bb" });
    items.Add(new Item { OrderId = 2, ItemId = 3, ItemName = "cc" });

    return items;
    }

    private List GetTaxes()
    {
    List taxes = new List();
    taxes.Add(new Tax { ItemId = 1, TaxId = 1, TaxName = "ff" });
    taxes.Add(new Tax { ItemId = 1, TaxId = 2, TaxName = "gg" });
    taxes.Add(new Tax { ItemId = 2, TaxId = 3, TaxName = "hh" });

    return taxes;
    }

    public class Order
    {
    public int OrderId { get; set; }
    public string OrderName { get; set; }
    }

    public class Item
    {
    public int OrderId { get; set; }
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    }

    public class Tax
    {
    public int ItemId { get; set; }
    public int TaxId { get; set; }
    public string TaxName { get; set; }
    }
    }
    View
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21 @{
    Layout = null;
    }






    Index



    @using (Html.BeginForm("GenerateXmlReport", "Home", FormMethod.Post))
    {


    }



    Generated XML
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27


    1
    a
    1
    aa
    1
    ff


    1
    a
    1
    aa
    1
    gg


    1
    a
    2
    bb
    2
    hh

    1
    idnkx user

    PARTH

    To export data into XML in ASP.NET, we can follow below approach.

    CONTROLLER ACTION METHOD

    public void ExportToXML()
    {
    var data = db.PersonalDetails.ToList();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;
    filename=itfunda.xml");
    Response.ContentType = "text/xml";

    var serializer = new
    System.Xml.Serialization.XmlSerializer(data.GetType());
    serializer.Serialize(Response.OutputStream, data);
    }
    In the above action method, we are getting the data from the database and setting almost same properties of the Response object that we have set into previous post.

    The changes are following

    we are changing the name of the file to download
    setting the ContentType to “text/xml”
    using XmlSerializer to Serialize the data into XML and sending to the Response OutputStream.

Leave a Reply
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let's Connect