Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with displaying data from XML file using ASP.NET MVC

Hello,

I'm trying to display data from an XML file with an APS.NET MVC project but running into a few issues in the code below, with the following error messages:

The type or namespace name 'CustomerExample' could not be found (are you missing a using directive or an assembly reference?) in File DisplayXMl.cshtml, the project's name was from CustomerExample to CustomerExample1, and no where in the application CustomerExample is still mentioned.

I am also receiving 3 additional errors in the CustomerController.cs file:

2. A namespace cannot directly contain members such as fields or methods.  on line " public List<CustomerModel> ReturnData()"

3. 'Server' does not contain a definition for 'MapPath'. on line " string xmldata = Server.MapPath("/XMLFile/CustomerData.xml");"

4. Cannot implicitly convert type 'int' to 'string'. on line " Contact = Convert.ToInt32(rows[2].ToString()),"

How do I fixed those errors, unfortunately can not upload the project as a zip file.



CustomerController.cs file:

using CustomerExample1.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CustomerExample1.Controllers
{
    public class CustomerController : Controller
    {

        public ActionResult DisplayXMl()
        {
            var data = new List<CustomerModel>();
            data = ReturnData();
            return View(data);
        }

        private List<CustomerModel> ReturnData()  
        {
            throw new NotImplementedException();
        }
    }

    public List<CustomerModel> ReturnData() *****Error 2
    {
        string xmldata = Server.MapPath("/XMLFile/CustomerData.xml");  *****Error 3
        DataSet ds = new DataSet();
        ds.ReadXml(xmldata);

        var customerlist = new List<CustomerModel>();

        customerlist = (from rows in ds.Tables[0].AsEnumerable()
                        select new CustomerModel
                        {
                            CustomerID = Convert.ToInt32(rows[0].ToString()),
                            CustomerName = rows[1].ToString(),
                            Contact = Convert.ToInt32(rows[2].ToString()),  *****Error 4

                        }).ToList();
        return customerlist;
    }
}

Open in new window


Thanks,


Victor
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Error 2: Your method can not exist outside a class.
Error 3: User Server.MapPath inside a controller method.
Error 4: Most likely Contact is a string type not an integer.

To fix use code below and change Contact type to int:
    public class CustomerController : Controller
    {

        public ActionResult DisplayXMl()
        {
            var data = new List<CustomerModel>();
            data = ReturnData();
            return View(data);
        }

        public List<CustomerModel> ReturnData()  
        {
			string xmldata = Server.MapPath("/XMLFile/CustomerData.xml");
			DataSet ds = new DataSet();
			ds.ReadXml(xmldata);

			var customerlist = new List<CustomerModel>();

			customerlist = (from rows in ds.Tables[0].AsEnumerable()
							select new CustomerModel
							{
								CustomerID = Convert.ToInt32(rows[0].ToString()),
								CustomerName = rows[1].ToString(),
								Contact = Convert.ToInt32(rows[2].ToString()),

							}).ToList();
			return customerlist;
        }
    }

Open in new window

Avatar of Victor  Charles

ASKER

Hi,

Thanks for your solution, unfortunately I ran into another error message in the Views section, line   @foreach (var item in Model) .

Error: System.NullReferenceException: Object reference not set to an instance of an object'
System.Web.Mvc.WebViewPage<Tmodel>.Model.get returned null.

How do I fix this error?

********************************************
@{
    ViewBag.Title = "ViewXML";
}
@using CustomerExample.Models;
@model List<CustomerExample.Models.CustomerModel>
<h2>Display Data in Webpage</h2>
<table class="table table-responsive table-bordered">
    <thread style="background-color:Highlight">
        <tr>
            <th>Customer ID</th>
            <th>Customer Name</th>
            <th>Contact</th>
        </tr>
    </thread>
    <tbody>
        @foreach (var item in Model) *Error Line*******************
        {
            <tr>
                <td>@item.CustomerID</td>
                <td>@item.CustomerName</td>
                <td>@item.Contact</td>
            </tr>
        }
    </tbody>
</table>
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.