Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:32:14+00:00 2026-06-06T16:32:14+00:00

I am posting XmlDocument to ApiController (from windows service, service is working fine, it

  • 0

I am posting XmlDocument to ApiController (from windows service, service is working fine, it is posting correct, I used it in wcf web API), but XML is always null, what am I doing wrong?

I can post some class, such in tutorials, or Get any data and everything will be ok, but I can’t post XmlDocument.

public class XmlController : ApiController
{
    public void PostXml(XmlDocument xml)
    {
       // code
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:32:16+00:00Added an answer on June 6, 2026 at 4:32 pm

    I’ve found a solution:

    We need to use inheritance to inherit MediaTypeFormatter

    public class XmlMediaTypeFormatter : MediaTypeFormatter
    {
        public XmlMediaTypeFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));      
        }
    
        public override System.Threading.Tasks.Task<object> ReadFromStreamAsync(Type type, Stream stream,
             HttpContentHeaders contentHeaders,
             IFormatterLogger formatterLogger)
        {
            var taskCompletionSource = new TaskCompletionSource<object>();
            try
            {
                var memoryStream = new MemoryStream();
                stream.CopyTo(memoryStream);
                var s = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
    
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(s);
    
                taskCompletionSource.SetResult(xmlDoc);
            }
            catch (Exception e)
            {
                taskCompletionSource.SetException(e);
            }
            return taskCompletionSource.Task;
        }
    
        public override bool CanReadType(Type type)
        {
            return type == typeof(XmlDocument);
        }
    
        public override bool CanWriteType(Type type)
        {
            return false;
        }
    }
    

    Then register it in Global.asax:

    GlobalConfiguration.Configuration.Formatters.Insert(0, new XmlMediaTypeFormatter());
    

    Controller:

    public HttpResponseMessage PostXml([FromBody] XmlDocument xml)
        {//code...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Picture posting NOT working with Facebook Graph API The message is posted but the
posting from an app (iOS android etc..) not a web form. class someImage(models.Model): image
I'm afraid I've not got much experience with posting documents (e.g. XML) over web-servers,
I'm posting to the auth.net web service to create a credit card transaction. In
I have now working updatable news using xml, but whenever i refresh the 1st
When posting from JS, PHP (5.3.8) returns some line returns in excess. For example:
When posting back to my controller my model is populated with correct values and
Below am posting my jsp page. home.jsp <%@taglib uri=/WEB-INF/struts-bean.tld prefix=bean%> <%@taglib uri=/WEB-INF/struts-html.tld prefix=html %>
I am posting pictures from a facebook application to a facebook fan page album
Posting non-latin based languages with ajax + jquery doesn't save to mysql the correct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.