Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


MVC4 c# passing XDocument

Posted on 2014-03-16
Medium Priority
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 64

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 64

Accepted Solution

Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…

670 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