Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public IEnumerable<Word> GetWords()
{
    return db.Words.ToList();
}


I need this output in XML using webapi2;

What I have tried:

i already google and try
C#
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "text/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);


It gives data only in JSON.

also I update following package:
Microsoft.AspNetCore.Mvc.Formatters.Xml


But how can I get it in XML.
Thanks in advance.
Posted
Updated 6-Sep-16 22:42pm
v2

1 solution

You can use
C#
Configuration.Formatters.XmlFormatter

C#
public IHttpActionResult Get()
{
 //Your code to get the result object
  return Content(HttpStatusCode.OK, results, Configuration.Formatters.XmlFormatter);
}
 
Share this answer
 
Comments
Hasan Habib Surzo 8-Sep-16 2:45am    
public IHttpActionResult GetWords()
{
//Your code to get the result object
IEnumerable<<word>> word = db.Words.ToList();
return Content(HttpStatusCode.OK, word, Configuration.Formatters.XmlFormatter);
}
still not working for Xml but it works for JSON.
NaibedyaKar 8-Sep-16 2:59am    
Strange!! This is working fine for me..
This is my code.

[Route("api/Products/GetPackCountsTest")]
[HttpGet]
public IHttpActionResult GetPackCountsTest()
{
var products = _productRepository.GetPackCounts();
return Content(HttpStatusCode.OK, products, Configuration.Formatters.XmlFormatter);
}
NaibedyaKar 8-Sep-16 3:01am    
Can you try with this.

public HttpResponseMessage Get()
{
var results = _productRepository.GetPackCounts();
return Request.CreateResponse(HttpStatusCode.OK, results, new XmlMediaTypeFormatter());
}
Hasan Habib Surzo 8-Sep-16 6:13am    
public HttpResponseMessage GetWords()
{
var results = db.Words.ToList();
return Request.CreateResponse(HttpStatusCode.OK, results, new XmlMediaTypeFormatter());
}

still problem.
Please check with google chrome.
should i start any service or register anything in WebApiConfig.cs?

also I update following package:
Microsoft.AspNetCore.Mvc.Formatters.Xml

Error :
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<error>
<message>The request is invalid.


if I try
[Route("api/WordsApi/GetWords")]
public IHttpActionResult GetWords()
{
var results = db.Words.ToList();
return Content(HttpStatusCode.OK, results, Configuration.Formatters.XmlFormatter);
}

Error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<error>
<message>An error has occurred.
NaibedyaKar 8-Sep-16 6:27am    
Can you check this

http://forums.asp.net/post/5531588.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900