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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:48:01+00:00 2026-05-17T21:48:01+00:00

I have a custom Fraction class, which I’m using throughout my whole project. It’s

  • 0

I have a custom Fraction class, which I’m using throughout my whole project. It’s simple, it consists of a single constructor, accepts two ints and stores them. I’d like to use the DataContractSerializer to serialize my objects used in my project, some of which include Fractions as fields. Ideally, I’d like to be able to serialize such objects like this:

<Object>
    ...
    <Frac>1/2</Frac> // "1/2" would get converted back into a Fraction on deserialization.
    ...
</Object>

As opposed to this:

<Object>
    ...
    <Frac>
        <Numerator>1</Numerator>
        <Denominator>2</Denominator>
    </Frac>
    ...
</Object>

Is there any way to do this using DataContracts?

I’d like to do this because I plan on making the XML files user-editable (I’m using them as input for a music game, and they act as notecharts, essentially), and want to keep the notation as terse as possible for the end user, so they won’t need to deal with as many walls of text.

EDIT: I should also note that I currently have my Fraction class as immutable (all fields are readonly), so being able to change the state of an existing Fraction wouldn’t be possible. Returning a new Fraction object would be OK, though.

  • 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-17T21:48:01+00:00Added an answer on May 17, 2026 at 9:48 pm

    If you add a property that represents the Frac element and apply the DataMember attribute to it rather than the other properties you will get what you want I believe:

    [DataContract]
    public class MyObject {
        Int32 _Numerator;
        Int32 _Denominator;
        public MyObject(Int32 numerator, Int32 denominator) {
            _Numerator = numerator;
            _Denominator = denominator;
        }
        public Int32 Numerator {
            get { return _Numerator; }
            set { _Numerator = value; }
        }
        public Int32 Denominator {
            get { return _Denominator; }
            set { _Denominator = value; }
        }
        [DataMember(Name="Frac")]
        public String Fraction {
            get { return _Numerator + "/" + _Denominator; }
            set {
                String[] parts = value.Split(new char[] { '/' });
                _Numerator = Int32.Parse(parts[0]);
                _Denominator = Int32.Parse(parts[1]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Silverlight 4 project I have a class that extends Canvas: public class
I have custom coded several enterprise applications for mid to large organizations to use
I have custom errors configured in my web.config, but IIS 6.0 is returning the
Can you have custom client-side javascript Validation for standard ASP.NET Web Form Validators? For
I have a few models that need to have custom find conditions placed on
hi i would like to create a custom calendar, this calendar will have custom
I have a custom validation function in JavaScript in a user control on a
I have a custom built ajax [div] based dynamic dropdown. I have an [input]
I have a custom control that implements IPostBackEventHandler. Some client-side events invoke __doPostBack(controlID, eventArgs).
I have a custom installer action that updates the PATH environment, and creates an

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.