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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:57:38+00:00 2026-06-03T06:57:38+00:00

My question is so simple. Imagine we have a Foo class. and has a

  • 0

My question is so simple.

Imagine we have a Foo class. and has a T property.

public class Foo<T>
{
    public T Property { get; set; }
}

I want to implement in it IXmlSerializable and my problem is I have no idea to imitate the standard XmlSerialization. I really have no idea about how to write this property like the standart XmlSerlalization.

UPDATE: I did this and it’s not working

public class Foo<T> : IFoo
    where T : IXmlSerializable
{
    public T Value { get; set; }
}

public class FooA : Foo<string> { }
public class FooB : Foo<int> { }
public class FooC : Foo<List<Double>> { }

When I serialized this classes (A, B and C). I wanna have something like this:

Using first class:

<FooA>
    <Value>StringDemo</Value>
</FooA>

Using second class:

<FooB>
    <Value>2</Value>
</FooB>

Using third class:

<FooC>
    <Value>
        <ArrayOfDouble xsi:..>
            <Double>3.1416</Double>
            <Double>4.2</Double>
        </ArrayOfDouble>
    </Value>
</FooC>

Something like this is what I wanted, I don’t want to implement in all of this a custom XmlSerializer, I mean the default which the standard serialization use.

  • 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-03T06:57:40+00:00Added an answer on June 3, 2026 at 6:57 am

    You would need to implement IXmlSerializable in Foo<T>, and T (using where T : IXmlSerializable. The implementation in Foo<T> would then be responsible for writing a wrapper element with type information (in WriteXml) and call WriteXml on T, and recreate the instance from the type information (in ReadXml) and call ReadXml on T.

    Basic types like String and Double is not IXmlSerializable, so you cant have where T : IXmlSerializable if you want to support them. You can also provide default implementations of ReadXml and WriteXml. This works since you need to create a Foo<T> before calling ReadXml, forcing you to know T.

    public class Foo<T> : IXmlSerializable {
        public T Value { get; set; }
    
        XmlSchema IXmlSerializable.GetSchema() {
            return GetSchema();
        }
    
        protected virtual XmlSchema GetSchema() {
            return null;
        }
    
        public virtual void ReadXml(XmlReader reader) {
            reader.ReadStartElement();
            var xmlSerializer = new XmlSerializer(typeof(T));
            Value = (T)xmlSerializer.Deserialize(reader);
            reader.ReadEndElement();
        }
    
        public virtual void WriteXml(XmlWriter writer) {
            var xmlSerializer = new XmlSerializer(typeof(T));
            xmlSerializer.Serialize(writer, Value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok simple question. I have a JSF application, containing a login page. The problem
Just imagine you have the following class [DataContract] public class NamedList { [DataMember] public
Let's say I have a simple class Cat in C#, with a Name property
I think this is a simple question. Imagine you have a page that initializes
yeah :( simple question * blush * Lets imagine i have the following enumeration:-
I have rather simple, I imagine, question about CRTP , but I cannot seem
Question simple and quick: I have started to use Netbeans to write some code
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew
Simple question... How am I going to reproduce Javascript bugs if I don't have
Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out

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.