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

  • Home
  • SEARCH
  • 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 6687313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:16:27+00:00 2026-05-26T05:16:27+00:00

Suppose we have a class which can be serialized/deserialized by XmlSerializer. It would be

  • 0

Suppose we have a class which can be serialized/deserialized by XmlSerializer. It would be like so:

[XmlRoot("ObjectSummary")]
public class Summary
{
     public string Name {get;set;}
     public string IsValid {get;set;}
}

We have an xml which will be like so:

<ObjectSummary>
   <Name>some name</Name>
   <IsValid>Y</IsValid>
<ObjectSummary>

Using of boolean property IsValid instead of string property is much better decision, but in this case we need to add some additional logic to convert data from string to bool.

The simple and direct way to solve this problem is to use additional property and put some conversion logic into the IsValid getter.

Can anyone suggest a better decision? To use a type converter in attributes somehow or something similar?

  • 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-26T05:16:27+00:00Added an answer on May 26, 2026 at 5:16 am

    Treat the node as a custom type:

    [XmlRoot("ObjectSummary")]
    public class Summary
    {
        public string Name {get;set;}
        public BoolYN IsValid {get;set;}
    }
    

    Then implement IXmlSerializable on the custom type:

    public class BoolYN : IXmlSerializable
    {
        public bool Value { get; set }
    
        #region IXmlSerializable members
    
        public System.Xml.Schema.XmlSchema GetSchema() {
            return null;
        }
    
        public void ReadXml(System.Xml.XmlReader reader) {
            string str = reader.ReadString();
            reader.ReadEndElement();
    
            switch (str) {
                case "Y":
                    this.Value = true;
                    break;
                case "N":
                    this.Value = false;
                    break;
            }
        }
    
        public void WriteXml(System.Xml.XmlWriter writer) {
            string str = this.Value ? "Y" : "N";
    
            writer.WriteString(str);
            writer.WriteEndElement();
        }
    
        #endregion
    }
    

    You can even make that custom class a struct instead, and provide implicit conversions between it and bool to make it even more “transparent”.

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

Sidebar

Related Questions

Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have one class Person ……… public class Person { public string Name
Suppose I have simple class Order, that have a TotalPrice calculated property, which can
Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Suppose I have a class like below: public static class Foo { public static
Suppose I have some class which has a property actor_ of type Actor .
Suppose I have a LimitedValue class which holds a value, and is parameterized on
Suppose I have a class A which depends on 3 other classes X, Y
Suppose I have a file X.h which defines a class X, whose methods are
Suppose I have: class Foo { ... }; class Bar : public Foo {

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.