Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC4 c# passing XDocument

Posted on 2014-03-16
5
Medium Priority
?
644 Views
Last Modified: 2014-03-16
Hello,

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?
0
Comment
Question by:HugoHiasl
  • 3
  • 2
5 Comments
 
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

0
 
LVL 12

Author Comment

by:HugoHiasl
ID: 39932583
No. I get the described error message.
0
 
LVL 64

Accepted Solution

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

Author Closing Comment

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

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

Thanks a lot :-)
0
 
LVL 64

Expert Comment

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

886 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