Hi rani,
You nreed to configure the OutputFormatters in startup file.
Next set XmlRootAttribute to the Model class and XmlElementAttribute to the property.
Check this example. Now please take its reference and correct your code.
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(options =>
{
options.OutputFormatters.RemoveType(typeof(SystemTextJsonOutputFormatter));
options.ReturnHttpNotAcceptable = true;
}).AddXmlSerializerFormatters();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Model
[System.Xml.Serialization.XmlRoot("Customers")]
public class CustomerModel
{
[System.Xml.Serialization.XmlElement("Customer")]
public List<Customer> Customers { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Web API Controller
[ApiController]
public class CustomerAPIController : ControllerBase
{
[Route("api/CustomerAPI/GetCustomers")]
[HttpGet]
public IActionResult GetCustomers()
{
CustomerModel model = new CustomerModel();
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { Id = 1, Name = "John Hammond", Country = "United States" });
customers.Add(new Customer { Id = 2, Name = "Mudassar Khan", Country = "India" });
customers.Add(new Customer { Id = 3, Name = "Suzanne Mathews", Country = "France" });
customers.Add(new Customer { Id = 4, Name = "Robert Schidner", Country = "Russia" });
model.Customers = customers;
return Ok(model);
}
}
Namespaces
using System.Data;
using System.IO;
using System.Net.Http;
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
string apiUrl = "http://localhost:9090/api/CustomerAPI/GetCustomers";
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(apiUrl).Result;
string result = "";
DataTable dt = new DataTable();
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
StringReader sr = new StringReader(result);
DataSet ds = new DataSet();
ds.ReadXml(sr);
dt = ds.Tables["Customer"];
}
return View(dt);
}
}
View
@model System.Data.DataTable
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
</tr>
@foreach (System.Data.DataRow dr in Model.Rows)
{
<tr>
<td>@dr["Id"]</td>
<td>@dr["Name"]</td>
<td>@dr["Country"]</td>
</tr>
}
</table>
</body>
</html>
Generated XML