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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:18:44+00:00 2026-05-10T21:18:44+00:00

Once a programmer decides to implement IXmlSerializable , what are the rules and best

  • 0

Once a programmer decides to implement IXmlSerializable, what are the rules and best practices for implementing it? I’ve heard that GetSchema() should return null and ReadXml should move to the next element before returning. Is this true? And what about WriteXml – should it write a root element for the object or is it assumed that the root is already written? How should child objects be treated and written?

Here’s a sample of what I have now. I’ll update it as I get good responses.

public class MyCalendar : IXmlSerializable {     private string _name;     private bool _enabled;     private Color _color;     private List<MyEvent> _events = new List<MyEvent>();       public XmlSchema GetSchema() { return null; }      public void ReadXml(XmlReader reader)     {         if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == 'MyCalendar')         {             _name    = reader['Name'];             _enabled = Boolean.Parse(reader['Enabled']);             _color   = Color.FromArgb(Int32.Parse(reader['Color']));              if (reader.ReadToDescendant('MyEvent'))             {                 while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == 'MyEvent')                 {                     MyEvent evt = new MyEvent();                     evt.ReadXml(reader);                     _events.Add(evt);                 }             }             reader.Read();         }     }      public void WriteXml(XmlWriter writer)     {         writer.WriteAttributeString('Name',    _name);         writer.WriteAttributeString('Enabled', _enabled.ToString());         writer.WriteAttributeString('Color',   _color.ToArgb().ToString());          foreach (MyEvent evt in _events)         {             writer.WriteStartElement('MyEvent');             evt.WriteXml(writer);             writer.WriteEndElement();         }     } }  public class MyEvent : IXmlSerializable {     private string _title;     private DateTime _start;     private DateTime _stop;       public XmlSchema GetSchema() { return null; }      public void ReadXml(XmlReader reader)     {         if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == 'MyEvent')         {             _title = reader['Title'];             _start = DateTime.FromBinary(Int64.Parse(reader['Start']));             _stop  = DateTime.FromBinary(Int64.Parse(reader['Stop']));             reader.Read();         }     }      public void WriteXml(XmlWriter writer)     {         writer.WriteAttributeString('Title', _title);         writer.WriteAttributeString('Start', _start.ToBinary().ToString());         writer.WriteAttributeString('Stop',  _stop.ToBinary().ToString());     } } 

Corresponding Sample XML

<MyCalendar Name='Master Plan' Enabled='True' Color='-14069085'>     <MyEvent Title='Write Code' Start='-8589241828854775808' Stop='-8589241756854775808' />     <MyEvent Title='???' Start='-8589241828854775808' Stop='-8589241756854775808' />     <MyEvent Title='Profit!' Start='-8589247048854775808' Stop='-8589246976854775808' /> </MyCalendar> 
  • 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. 2026-05-10T21:18:45+00:00Added an answer on May 10, 2026 at 9:18 pm

    Yes, GetSchema() should return null.

    IXmlSerializable.GetSchema Method This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return a null reference (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.

    For both read and write, the object element has already been written, so you don’t need to add an outer element in write. For example, you can just start reading/writing attributes in the two.

    For write:

    The WriteXml implementation you provide should write out the XML representation of the object. The framework writes a wrapper element and positions the XML writer after its start. Your implementation may write its contents, including child elements. The framework then closes the wrapper element.

    And for read:

    The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.

    When this method is called, the reader is positioned at the start of the element that wraps the information for your type. That is, just before the start tag that indicates the beginning of a serialized object. When this method returns, it must have read the entire element from beginning to end, including all of its contents. Unlike the WriteXml method, the framework does not handle the wrapper element automatically. Your implementation must do so. Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.

    I’ll agree that is a little unclear, but it boils down to ‘it is your job to Read() the end-element tag of the wrapper’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's worth noting that in the first case they're only… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer Update Looks like Apple made an IETF draft proposal, and… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer Are you managing your context with an HttpHandler or HttpModule?… May 11, 2026 at 11:30 pm

Related Questions

There is a similar question going around, but it just got the same old
I'm part of a software development company where we do custom developed applications for
I am asking this question because I know there are a lot of well-read
I'm a programmer dammit, I should be allowed to ask these kinds of IT

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.