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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:01:58+00:00 2026-06-16T12:01:58+00:00

I have got the following code: BaseContent.cs public class BaseContent { // Some auto

  • 0

I have got the following code:

BaseContent.cs

public class BaseContent
{
   // Some auto properties
}

News.cs

public class News : BaseContent
{
   // Some more auto properties
}

Events.cs

public class Event : BaseContent
{
   // Some more auto properites
}

GenericResponse.cs

public class GenericResponse<T> 
{
  [XmlArray("Content")]
  [XmlArrayItem("NewsObject", typeof(News)]
  [XmlArrayItem("EventObject", typeof(Event)]
  public List<T> ContentItems { get; set; }
}

NewsResponse.cs

public class NewsResponse : GenericResponse<News> {}

EventResponse.cs

public class EventResponse : GenericResponse<Event> {}

As you can see, I have a base class BaseContent and two classes deriving from it. Next I have a generic response class, since the structure of the xml-files is always the same, but they differ in some properties.

I thought I can specify with the [XmlArrayItem] which name to use for a specific class. But now I get the error:

System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0012: The type ‘System.Object’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

I can not add this reference, because I’m working on a Windows 8 App.

If I comment out one of the [XmlArrayItem] it is working well.

Anyone got an idea to solve this problem?

Update
I can not use DataContractSerializer, because I have to use XmlAttributes

  • 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-06-16T12:01:59+00:00Added an answer on June 16, 2026 at 12:01 pm

    Edit: Feel free to download the demo project

    You didn’t provide all the properties for the objects, so allow me to add some – just as an example:

    public class BaseContent
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
    }
    
    [XmlType(TypeName = "EventObject")]
    public class Event : BaseContent
    {
        [XmlAttribute("EventId")]
        public int EventId { get; set; }
    }
    
    [XmlType(TypeName = "NewsObject")]
    public class News : BaseContent
    {
        [XmlAttribute("NewsId")]
        public int NewsId { get; set; }
    }
    

    GenericResponse.cs could be defined this way – no need to specify the typeof for the array items:

    public class GenericResponse<T>
    {
        [XmlArray("Content")]
        public List<T> ContentItems { get; set; }
    
        public GenericResponse()
        {
            this.ContentItems = new List<T>();
        }
    }
    

    And then you have the response classes:

    public class EventResponse : GenericResponse<Event>
    {
    }
    
    public class NewsResponse : GenericResponse<News>
    {
    }
    

    Example 1: Serialize an EventResponse object

    var response = new EventResponse
    {
        ContentItems = new List<Event> 
        {
            new Event {
                EventId = 1,
                Name = "Event 1"
            },
    
            new Event {
                EventId = 2,
                Name = "Event 2"
            }
        }
    };
    
    string xml = XmlSerializer<EventResponse>.Serialize(response);
    

    Output XML:

    <?xml version="1.0" encoding="utf-8"?>
    <EventResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Content>
            <EventObject Name="Event 1" EventId="1" />
            <EventObject Name="Event 2" EventId="2" />
        </Content>
    </EventResponse>
    

    If you try the same with NewsResponse it will work fine. BTW I’m using my generic XmlSerializer, click on the link to know more about it.

    XmlSerializer.cs:

    /// <summary>
    /// XML serializer helper class. Serializes and deserializes objects from/to XML
    /// </summary>
    /// <typeparam name="T">The type of the object to serialize/deserialize.
    /// Must have a parameterless constructor and implement <see cref="Serializable"/></typeparam>
    public class XmlSerializer<T> where T: class, new()
    {
        /// <summary>
        /// Deserializes a XML string into an object
        /// Default encoding: <c>UTF8</c>
        /// </summary>
        /// <param name="xml">The XML string to deserialize</param>
        /// <returns>An object of type <c>T</c></returns>
        public static T Deserialize(string xml)
        {
            return Deserialize(xml, Encoding.UTF8, null);
        }
    
        /// <summary>
        /// Deserializes a XML string into an object
        /// Default encoding: <c>UTF8</c>
        /// </summary>
        /// <param name="xml">The XML string to deserialize</param>
        /// <param name="encoding">The encoding</param>
        /// <returns>An object of type <c>T</c></returns>
        public static T Deserialize(string xml, Encoding encoding)
        {
            return Deserialize(xml, encoding, null);
        }
    
        /// <summary>
        /// Deserializes a XML string into an object
        /// </summary>
        /// <param name="xml">The XML string to deserialize</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
        /// <returns>An object of type <c>T</c></returns>
        public static T Deserialize(string xml, XmlReaderSettings settings)
        {
            return Deserialize(xml, Encoding.UTF8, settings);
        }
    
        /// <summary>
        /// Deserializes a XML string into an object
        /// </summary>
        /// <param name="xml">The XML string to deserialize</param>
        /// <param name="encoding">The encoding</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
        /// <returns>An object of type <c>T</c></returns>
        public static T Deserialize(string xml, Encoding encoding, XmlReaderSettings settings)
        {
            if (string.IsNullOrEmpty(xml))
                throw new ArgumentException("XML cannot be null or empty", "xml");
    
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    
            using (MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(xml)))
            {
                using (XmlReader xmlReader = XmlReader.Create(memoryStream, settings))
                {
                    return (T) xmlSerializer.Deserialize(xmlReader);
                }
            }
        }
    
        /// <summary>
        /// Deserializes a XML file.
        /// </summary>
        /// <param name="filename">The filename of the XML file to deserialize</param>
        /// <returns>An object of type <c>T</c></returns>
        public static T DeserializeFromFile(string filename)
        {
            return DeserializeFromFile(filename, new XmlReaderSettings());
        }
    
        /// <summary>
        /// Deserializes a XML file.
        /// </summary>
        /// <param name="filename">The filename of the XML file to deserialize</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
        /// <returns>An object of type <c>T</c></returns>
        public static T DeserializeFromFile(string filename, XmlReaderSettings settings)
        {
            if (string.IsNullOrEmpty(filename))
                throw new ArgumentException("filename", "XML filename cannot be null or empty");
    
            if (! File.Exists(filename))
                throw new FileNotFoundException("Cannot find XML file to deserialize", filename);
    
            // Create the stream writer with the specified encoding
            using (XmlReader reader = XmlReader.Create(filename, settings))
            {
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                return (T) xmlSerializer.Deserialize(reader);
            }
        }
    
        /// <summary>
        /// Serialize an object
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <returns>A XML string that represents the object to be serialized</returns>
        public static string Serialize(T source)
        {
            // indented XML by default
            return Serialize(source, null, GetIndentedSettings());
        }
    
        /// <summary>
        /// Serialize an object
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="namespaces">Namespaces to include in serialization</param>
        /// <returns>A XML string that represents the object to be serialized</returns>
        public static string Serialize(T source, XmlSerializerNamespaces namespaces)
        {
            // indented XML by default
            return Serialize(source, namespaces, GetIndentedSettings());
        }
    
        /// <summary>
        /// Serialize an object
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
        /// <returns>A XML string that represents the object to be serialized</returns>
        public static string Serialize(T source, XmlWriterSettings settings)
        {
            return Serialize(source, null, settings);
        }
    
        /// <summary>
        /// Serialize an object
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="namespaces">Namespaces to include in serialization</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
        /// <returns>A XML string that represents the object to be serialized</returns>
        public static string Serialize(T source, XmlSerializerNamespaces namespaces, XmlWriterSettings settings)
        {
            if (source == null)
                throw new ArgumentNullException("source", "Object to serialize cannot be null");
    
            string xml = null;
            XmlSerializer serializer = new XmlSerializer(source.GetType());
    
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings))
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    x.Serialize(xmlWriter, source, namespaces);
    
                    memoryStream.Position = 0; // rewind the stream before reading back.
                    using (StreamReader sr = new StreamReader(memoryStream))
                    {
                        xml = sr.ReadToEnd();
                    } 
                }
            }
    
            return xml;
        }
    
        /// <summary>
        /// Serialize an object to a XML file
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="filename">The file to generate</param>
        public static void SerializeToFile(T source, string filename)
        {
            // indented XML by default
            SerializeToFile(source, filename, null, GetIndentedSettings());
        }
    
        /// <summary>
        /// Serialize an object to a XML file
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="filename">The file to generate</param>
        /// <param name="namespaces">Namespaces to include in serialization</param>
        public static void SerializeToFile(T source, string filename, XmlSerializerNamespaces namespaces)
        {
            // indented XML by default
            SerializeToFile(source, filename, namespaces, GetIndentedSettings());
        }
    
        /// <summary>
        /// Serialize an object to a XML file
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="filename">The file to generate</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
        public static void SerializeToFile(T source, string filename, XmlWriterSettings settings)
        {
             SerializeToFile(source, filename, null, settings);
        }
    
        /// <summary>
        /// Serialize an object to a XML file
        /// </summary>
        /// <param name="source">The object to serialize</param>
        /// <param name="filename">The file to generate</param>
        /// <param name="namespaces">Namespaces to include in serialization</param>
        /// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
        public static void SerializeToFile(T source, string filename, XmlSerializerNamespaces namespaces, XmlWriterSettings settings)
        {
            if (source == null)
                throw new ArgumentNullException("source", "Object to serialize cannot be null");
    
            XmlSerializer serializer = new XmlSerializer(source.GetType());
    
            using (XmlWriter xmlWriter = XmlWriter.Create(filename, settings))
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
                x.Serialize(xmlWriter, source, namespaces);
            }
        }
    
        #region Private methods
    
    
        private static XmlWriterSettings GetIndentedSettings()
        {
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
            xmlWriterSettings.Indent = true;
            xmlWriterSettings.IndentChars = "\t";
    
            return xmlWriterSettings;
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got following code: I have a enum: public enum HardwareInterfaceType { Gpib
I have got the following code #include <iostream> #include <string> template <typename T> class
I got the following code for my object (short version) : public class PluginClass
I got the following code to generate a DLL : public class QtObject :
Have got the following HAML code, combined with Markdown: %h2.slogan.align-center :markdown No more big
while compiling the following code i have got error @Override public void routeCustomerRequest (int
I have got the following jquery code which appends some html elements to the
I am having some Problems with JavaScript. I have got the following code: <html>
I have got the following code : <ul> <li class=jstree-leaf kids=0 range=5-7 name=mars public_id=mars_05
I have got the following very simple code: function init() { var articleTabs =

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.