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 3486198
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:59:59+00:00 2026-05-18T10:59:59+00:00

I have a model that I would like to serialize to an xml with

  • 0

I have a model that I would like to serialize to an xml with specific properties.

Model:

public class MyClassModel
{
    public int Id { get; set; }
    public DateTime updated { get; set; }
}

The Code in the controller action:

IList<MyClassModel> objects = getStuff();
return new XmlResult(jaPropEstates); //Asp.net mvc class that is inherited from ActionResult

XmlResult class

public class XmlResult : ActionResult
{
    private object objectToSerialize;

    /// <summary>
    /// Initializes a new instance of the <see cref="XmlResult"/> class.
    /// </summary>
    /// <param name="objectToSerialize">The object to serialize to XML.</param>
    public XmlResult(object objectToSerialize)
    {
        this.objectToSerialize = objectToSerialize;
    }

    /// <summary>
    /// Gets the object to be serialized to XML.
    /// </summary>
    public object ObjectToSerialize
    {
        get { return this.objectToSerialize; }
    }

    /// <summary>
    /// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream.
    /// </summary>
    /// <param name="context">The controller context for the current request.</param>
    public override void ExecuteResult(ControllerContext context)
    {
        if (this.objectToSerialize != null)
        {
            context.HttpContext.Response.Clear();
            var xs = new System.Xml.Serialization.XmlSerializer(this.objectToSerialize.GetType());
            context.HttpContext.Response.ContentType = "text/xml";
            xs.Serialize(context.HttpContext.Response.Output, this.objectToSerialize);
        }
    }
}

The output:

<ArrayOfMyClassModel>
   <MyClassModel>
      <Id>0</Id>
      <updated>0001-01-01T00:00:00</updated>
   </MyClassModel>
   <MyClassModel>
      <Id>2</Id>
      <updated>0001-01-01T00:00:00</updated>
   </MyClassModel>

I want it to be like this:

<?xml version="1.0" encoding="utf-8" ?> <!-- I want this -->
<listings xmlns="listings-schema"> <!-- I want ArrayOfMyClassModel to be renamed to this -->
    <property> <!-- I want MyClassModel to be renamed to property -->
      <Id>2</Id>
      <updated>0001-01-01T00:00:00</updated>
    </property>
</listings>

Note the difference as commented. How do I give my elements custom names?

  • 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. Editorial Team
    Editorial Team
    2026-05-18T11:00:00+00:00Added an answer on May 18, 2026 at 11:00 am

    Check out Proper way to implement IXmlSerializable. Gives you complete control over the XML serializer.

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

Sidebar

Related Questions

I have an ActiveRecord model that I would like to convert to xml, but
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
Specifically, I have a model that has a field like this pub_date = models.DateField(date
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a relationship in a Core Data model that feels like it wants
I have a php application that doesn't use the mvc model that i would
I have a model that picks up the choices defined in CHOICES like this:
I have a model House that has many boolean attributes, like has_fireplace , has_basement
I have a model that has several properties. The properties can be primitive (String)

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.