MVC4 c# passing XDocument

Posted on 2014-03-16
Last Modified: 2014-03-16

I created a XDocument element which shows exactly the XML that I need if I call the ToString() method.

Now I need to create a WEB API controller method to enable myself to call the controller and get exactly the XML of the XDocument.

I get an
"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'."

Open in new window

error if I try something like this:

        // GET api/values
        public XDocument Get()
            XDocument doc = new XDocument();
            doc.Add(new XElement("list"));

            <filling the XDocument>

            return doc;

Open in new window

How can I get this to work?
Question by:HugoHiasl
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 63

Expert Comment

by:Fernando Soto
ID: 39932564
Is not this code returning what you need?
// GET api/values
public XDocument Get()

Open in new window

LVL 12

Author Comment

ID: 39932583
No. I get the described error message.
LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 39932644
Please have a look at the solution [here] basically the same type of question.
LVL 12

Author Closing Comment

ID: 39932713
That brought me to the right solution...

I now send the Root-XElement instead of the XDocument. That works...

Thanks a lot :-)
LVL 63

Expert Comment

by:Fernando Soto
ID: 39932721
Not a problem, glad to help.

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question