Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:24:50+00:00 2026-05-11T08:24:50+00:00

I need advice on how to return a limited set of data from an

  • 0

I need advice on how to return a limited set of data from an MVC controller.

Lets say I have a class that is constructed like so:

public interface ICustomerExpose {    string Name {get; set;}    string State {get; set;} }  public interface ICustomer: ICustomerExpose {    int Id {get; set;}    string SSN {get; set;} }  public class Customer: ICustomer {    ... } 

In my MVC project I have a controller action that returns customer data. The project is actually more like a web service as there is no View associated with the data… we use the XmlResult (provided by the MVCContrib project). The controller action looks like this:

    // GET: /Customer/Show/5     public ActionResult Show(int id)     {         Customer customer = Customer.Load(id);          ...  // some validation work          return new XmlResult((ICustomerExpose)customer);     } 

The above controller code does not work like I want it to. What I want to happen is that only the Name and State properties are serialized and returned in the XmlResult. In practice the whole customer object is serialized including the data I definitely don’t want exposed.

I know the reason this doesn’t work: you can’t serialize an interface.

One idea floated around the office was to simply mark the properties Name and State as [XmlIgnore]. However, this doesn’t seem like a good solution to me. There might be other instances where I want to serialize those properties and marking the properties on the class this way prohibits me.

What is the best way to achieve my goal of only serializing the properties in the ICustomerExpose interface?

Addendum:

For those interested in what XmlResult does here are the relevant parts of it:

 public class XmlResult : ActionResult  {      private object _objectToSerialize;      public XmlResult(object objectToSerialize)      {         _objectToSerialize = objectToSerialize;      }       /// <summary>      /// Serialises the object that was passed into the constructor       /// to XML and writes the corresponding XML to the result stream.      /// </summary>      public override void ExecuteResult(ControllerContext context)      {          if (_objectToSerialize != null)          {             var xs = new XmlSerializer(_objectToSerialize.GetType());             context.HttpContext.Response.ContentType = 'text/xml';             xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize);          }      }  } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:24:50+00:00Added an answer on May 11, 2026 at 8:24 am

    I ended up just doing the XmlIgnore as co-workers suggested, even though this left me with some undesirable (or so I thought) behaviors.

    To get around the fact that XmlIgnore would continue hiding properties that I might want serialized later I asked another question trying to find a way to around that issue. Cheeso came up with a great answer making the XmlIgnore the best route (in my opinion) to take.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables, I need advice on how to get data from them.
I need advice on using stored procedures with Entity Framwork 4.x to return data
I need some advice with the Java Print API. I have a program that
i need some advice. i have 2 classes for a game that i am
I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
I really need advice on how to do the following. I have tried several
I think I have this working but I need advice. I'd like to know
I need some advice. I’m building an app that has a sequence of 4
I need and advice. This is my issue, I have N functions. var FirstOne
I need some advice here. I need to make a webservice, that can make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.