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
  • 3
  • 2
LVL 62

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 62

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 62

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now