Victor Charles
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/C ustomerDat a.xml");"
4. Cannot implicitly convert type 'int' to 'string'. on line " Contact = Convert.ToInt32(rows[2].To String()), "
How do I fixed those errors, unfortunately can not upload the project as a zip file.
CustomerController.cs file:
Thanks,
Victor
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/C
4. Cannot implicitly convert type 'int' to 'string'. on line " Contact = Convert.ToInt32(rows[2].To
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;
}
}
Thanks,
Victor
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.NullReferenceExcept ion: Object reference not set to an instance of an object'
System.Web.Mvc.WebViewPage <Tmodel>.M odel.get returned null.
How do I fix this error?
************************** ********** ********
@{
ViewBag.Title = "ViewXML";
}
@using CustomerExample.Models;
@model List<CustomerExample.Model s.Customer Model>
<h2>Display Data in Webpage</h2>
<table class="table table-responsive table-bordered">
<thread style="background-color:Hi ghlight">
<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>
Thanks for your solution, unfortunately I ran into another error message in the Views section, line @foreach (var item in Model) .
Error: System.NullReferenceExcept
System.Web.Mvc.WebViewPage
How do I fix this error?
**************************
@{
ViewBag.Title = "ViewXML";
}
@using CustomerExample.Models;
@model List<CustomerExample.Model
<h2>Display Data in Webpage</h2>
<table class="table table-responsive table-bordered">
<thread style="background-color:Hi
<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 TRIALMembers 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.
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:
Open in new window