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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:23:27+00:00 2026-05-22T23:23:27+00:00

I am trying to serialize a list that contains non-system types. Below is my

  • 0

I am trying to serialize a list that contains non-system types.

Below is my serialization code which is working fine on the top level. and returns a valid XmlDocument, but doesn’t seem to contain anything in a inner list.

I’ve looked around the net – and around SO – but can’t seem to find anything!

Any help much appreciated.

Code:

public static XmlDocument SerializeToXML<T>(List<T> list, string rootElement)
{
    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    XmlAttributes attr = new XmlAttributes();
    attr.XmlRoot = new XmlRootAttribute(rootElement);
    overrides.Add(typeof(List<T>), attr);

    XmlDocument xmlDoc = new XmlDocument();
    XPathNavigator nav = xmlDoc.CreateNavigator();
    using (XmlWriter writer = nav.AppendChild())
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<T>), overrides);

        ser.Serialize(writer, list);
    }
    return xmlDoc;
}

Code used to test:

[TestFixture]
public class BaseTesting
{
    [Test]
    public void test()
    {
        List<ListTestClass> list = new List<ListTestClass>();

        for (int i = 0; i < 20; i++)
        {
            list.Add(new ListTestClass() { intProp = 1, stringProp = "string1", dtProp = DateTime.Now });
        }

        XmlDocument doc = Beyond.Base.Util.XMLUtils.SerializeToXML<ListTestClass>(list, "root");
    }
}

public class ListTestClass
{
    public int intProp { get; set; }

    public string stringProp { get; set; }

    public DateTime dtProp { get; set; }

    [XmlElement("Inner",typeof(InnerListTestClass))]
    public InnerListTestClass inner { get { return new InnerListTestClass() { intProp = 1, stringProp = "string1", dtProp = DateTime.Now }; } }
}

public class InnerListTestClass
{
    public int intProp { get; set; }

    public string stringProp { get; set; }

    public DateTime dtProp { get; set; }
}

XML Output:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
</root>
  • 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-22T23:23:28+00:00Added an answer on May 22, 2026 at 11:23 pm

    inner property must have setter in order to be serializable.

    If you change it to

    public InnerListTestClass inner { get; set; }
    

    It will be serialized, as you expect it to.

    <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T01:57:07.1200742-07:00</dtProp>
    <Inner>
        <intProp>1</intProp>
        <stringProp>string1</stringProp>
        <dtProp>2011-06-07T01:57:07.1210743-07:00</dtProp>
    </Inner>
    </ListTestClass>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to serialize a Python list but got errors that it's not
When trying to serialize a type (a generic List<T> which T is a class
I am trying to serialize a large (~10**6 rows, each with ~20 values) list,
I'm trying to serialize a custom class that needs to use multiple elements of
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to serialize a class derived from List<> using DataContract. The problem is
I am trying to serialize a List<T> where T: EntityObject and would love to
I'm trying to use the DataContractJsonSerializer to serialize and deserialize a c# object that
I am trying to serialize a list of descendants. This is what I have
I'm trying the render an HTML list that looks like the following, using the

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.