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

  • SEARCH
  • Home
  • 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 6790375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:43:05+00:00 2026-05-26T17:43:05+00:00

I am trying to serialize a List<T> where T: EntityObject and would love to

  • 0

I am trying to serialize a List<T> where T: EntityObject and would love to leave out all the EntityKey and other EntityReference properties from the items in the list. Can this be done dynamically possibly using XmlAttributeOverrides?

As far as I can see, the XmlAttributeOverrides options only really point to the top level object ie the List<T> and not the T themselves, which is not very helpful to me.
Could anyone point me to a way to dynamically ignore properties of ArrayItems?

Here is a simple example I have been using that does not use EntityObjects but it should illustrate what I would like to do:

    public class Car
    {
        public String Make { get; set; }
        public String Model { get; set; }
        public Double EngineSize { get; set; }
    }

    [Test]
    public void WouldLoveToDynamicallyLeaveOutMembersOfArrayItems()
    {
        var cars = new List<Car>
                       {
                         new Car
                             {
                                 Make = "Ferrari",
                                 Model = "F1",
                                 EngineSize = 6000
                             },
                         new Car
                             {
                                 Make = "Williams",
                                 Model = "F1",
                                 EngineSize = 5500
                             }
                     };


        var attributeOverrides = new XmlAttributeOverrides();
        attributeOverrides.Add(typeof(Double), "EngineSize", new XmlAttributes {XmlIgnore = true});

        var xs = new XmlSerializer(cars.GetType(), attributeOverrides, new []{ typeof(Car) }, new XmlRootAttribute("cars"), "");
        var ms = new MemoryStream();
        xs.Serialize(ms, cars);
        ms.Position = 0;

        var sr = new StreamReader(ms);
        var result = sr.ReadToEnd();

        Assert.IsFalse(result.Contains("EngineSize"));

    }
  • 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-26T17:43:06+00:00Added an answer on May 26, 2026 at 5:43 pm

    Yes you can do that – the main error is you need typeof(Car) instead of typeof(double). With this, note that XmlAttributeOverrides does not just apply to the top-level onject.

    However, I’m not sure that is the easiest route. Firstly, note that you must store and re-use the serialiser when using XmlAttributeOverrides otherwise you will leak assemblies.

    I expect the main reason you want to do this is because you don’t want to edit he generated file. However, there is another way; in a separate file, in the right namespace, you can use:

    partial class Car {
        public bool ShouldSerializeEngineSize() { return false; }
    }
    

    Where “ShouldSerialize*” is a pattern recognised and used by XmlSerializer to control conditional serialization. The “partial” class is simply a way of combining two separate code files into a single type (and was designed for this generated-content scenario).

    This way, you dont need to mess with XmlAttributeOverrides, and you can use the simpler “new XmlSeralizer(Type)” (which has automatic caching per-type).

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

Sidebar

Related Questions

I'm trying to serialize a class derived from List<> using DataContract. The problem is
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to
I'm trying to serialize a list of objects I have in a particular format.
I am trying to serialize a list of dictionaries to a csv text file
I'm trying to serialize and deserialize an array list with a object inside: HairBirt
I am trying to serialize a couple of nested classes to and from an
I'm trying out XStream as a way to quickly serialize objects to Xml or
I'm trying to figure out the best way to save (serialize) and later open
I am trying to serialize a list of descendants. This is what I have

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.