Solved

MVC4 c# passing XDocument

Posted on 2014-03-16
5
584 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
No. I get the described error message.
0
 
LVL 62

Accepted Solution

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

Author Closing Comment

by:HugoHiasl
Comment Utility
That brought me to the right solution...

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

Thanks a lot :-)
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

7 Experts available now in Live!

Get 1:1 Help Now