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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:50:16+00:00 2026-05-15T14:50:16+00:00

If using custom XML Serialization ( IXmlSerialiable ), on a complex object that contains

  • 0

If using custom XML Serialization (IXmlSerialiable), on a complex object that contains properties with constituent complex objects which do NOT use custom IXmlSerializable interface, how do you specify, in the IXmlSerializable.ReadXml(XmlReader reader) method, that you want the deserializer to use the ordinary deserialization on those child properties?

NOTE: similar to this question

  • 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-15T14:50:17+00:00Added an answer on May 15, 2026 at 2:50 pm

    The IXmlSerializable is a bit tedious to implement since it’s pretty much an all or nothing approach given that you cannot select child types for normal XML serialization. However, if I understood you correctly you can achieve what you want by manually creating XmlSerializer for the types that do not implement IXmlSerializable.

    For example, if we start with two classes, Default that does not implement IXmlSerializable and Custom which does implement it.

    public class Default // Uses default XML Serialization
    {
        public int Count { get; set; }
    }
    
    public class Custom : IXmlSerializable
    {
        public int Count { get; set; }
    
        public XmlSchema GetSchema() { throw new NotImplementedException(); }
    
        public void ReadXml(XmlReader reader)
        {
            reader.ReadToDescendant("Count");
            this.Count = reader.ReadElementContentAsInt();
        }
    
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("Custom");
            writer.WriteElementString("Count", this.Count.ToString());
            writer.WriteEndElement();
        }
    }
    

    Then we create a third class Parent that has a child of each of the previous instances and implements IXmlSerializable in a way that calls ReadXml/WriteXml methods for the child that supports it and create default XML serializer for the other child.

    public class Parent : IXmlSerializable
    {
        public Parent()
        {
            this.Default = new Default { Count = 1 };
            this.Custom = new Custom { Count = 2 };
        }
    
        public Default Default { get; set; }
        public Custom Custom { get; set; }
    
        public XmlSchema GetSchema() { throw new NotImplementedException(); }
    
        public void ReadXml(XmlReader reader)
        {
            reader.ReadToFollowing("Custom");
            this.Custom = new Custom();
            this.Custom.ReadXml(reader);
    
            reader.ReadToFollowing("Default");
            var serializer = new XmlSerializer(typeof(Default));
            this.Default = (Default)serializer.Deserialize(reader);
        }
    
        public void WriteXml(XmlWriter writer)
        {
            this.Custom.WriteXml(writer);
    
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            new XmlSerializer(typeof(Default)).Serialize(writer, this.Default, ns);
        }
    }
    

    To make the example complete, a sample program that serializes and deserializes a Parent instance:

    static void Main()
    {
        var sb = new StringBuilder();
        var serializer = new XmlSerializer(typeof(Parent));
    
        serializer.Serialize(new StringWriter(sb), new Parent());
    
        Console.WriteLine(sb);
    
        var parent = (Parent)serializer.Deserialize(new StringReader(sb.ToString()));
    
        Console.WriteLine("Parent.Custom.Count: {0}", parent.Custom.Count);
        Console.WriteLine("Parent.Default.Count: {0}", parent.Default.Count);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using TabActivity with Custom Title. TabControl.xml contains android:focusable=true android:focusableInTouchMode=true When activity gets launched,
Currently, we use a giant configuration object that is serialized to/from XML. This has
I'm optimizing a custom object -> XML serialization utility, and it's all done and
My org uses a custom serialization technique, where we store objects iteratively using an
I'm using Schematron to validate instance XML documents inside a custom schema aware XML
I am using http://blog.frankel.ch/custom-loginmodule-in-tomcat tutorial for tomcat JAASRealm.I have added below in server.xml Realm
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I'm using custom class that subclass from UILabel. It works fine unless I change
I have an simple custom object called MyObject (a couple of basic properties and
I'm using Apache Axis2 and need to send a custom XML structure over SOAP

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.