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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:47:27+00:00 2026-05-30T06:47:27+00:00

Objects of the following class can be serialized to XML as intended, as long

  • 0

Objects of the following class can be serialized to XML as intended, as long as the actual type of the generic field Object is contained in the list of XmlElement attributes:

public class SerializedObject<T> : Serializable where T : Serializable
{
    [System.Xml.Serialization.XmlElement(Type = typeof(Weapon))]
    [System.Xml.Serialization.XmlElement(Type = typeof(Armor))]
    [System.Xml.Serialization.XmlElement(Type = typeof(QuestItem))]
    public T Object;

    public string ObjectId;
    public int ID;

    public SerializedObject() { }

    public SerializedObject(T _object)
    {
        Object = _object;
        ID = Object.Id;
        ObjectId = Object.ObjectId;
    }
}

The question is:

How can I serialize an object of this class, including the generic field Object without specifiying all possible types for T in XmlElement attributes?

  • 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-30T06:47:28+00:00Added an answer on May 30, 2026 at 6:47 am

    I ran into this too. What I did is create a wrapper class:

    public static XmlDocument SerializeToXmlDocument<XmlEntity>(XmlEntity o)
    {
        XmlDocument xdoc;
        SerializeWrapper<XmlEntity> wrapper = new SerializeWrapper<XmlEntity>();
        wrapper.XmlObject = o;
    
        XmlSerializer xs = new XmlSerializer(wrapper.GetType());
        using (MemoryStream ms = new MemoryStream())
        {
            xs.Serialize(ms, wrapper);
            xdoc = new XmlDocument();
            ms.Position = 0;
            xdoc.Load(ms);
        }
    
        return xdoc;
    }
    

    Here is the class used to wrap the object

    [XmlRoot("Root")]
    public class SerializeWrapper<TObject>
    {
        [XmlAttribute()]
        public string Name { get; set; }
    
        public TObject XmlObject { get; set; }
    }
    

    Now, you can just call it as:

    Weapon weapon = new Weapon()
    var xdoc = SerializeToXmlDocument<Weapon>(weapon);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I select an object's class in HQL? When I do the following:
i am trying to XML serialize a class object but having the following problem:
I have object XML serialized messages coming into a class called MessageRouter . The
I have a question on constant objects. In the following program: class const_check{ int
Consider the following simplified objects and relationships: public class Job{ int JobId; String name;
I have the following user resource: class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name
Suppose the following object structure: class Super {} class SubA extends Super {} class
Given the following object: public class Product { string Name {get;} int Quantity {get;}
Why does the following: class A(object): def __init__(self, var=[]): self._var = var print 'var
I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension

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.