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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:11:51+00:00 2026-05-21T10:11:51+00:00

I have a class with two parameters. One is the update time in Minutes

  • 0

I have a class with two parameters. One is the update time in Minutes and the other is the same value in seconds like this:

using System.Xml.Serialization;

[XmlRoot(ElementName="config")]
public class Config
{
    [XmlElement(ElementName = "update_mins")]
    public int Minutes 
    {
        get
        {
            return this.Seconds / 60;
        }
        set
        {
            this.Seconds = value * 60;
        }
    }

    [XmlElement(ElementName = "update_secs")]
    public int Seconds { get; set; }
}

I get xml strings that can have either update_mins or update_secs but not both. I need to be able to use this class to deserialize these strings, this works ok. I also need to be able to searalize these classes and send them on, however when I do the serailzation it includes both update_secs and update_mins. Like this:

<config>
  <update_mins>23</update_mins>
  <update_secs>1380</update_secs>
</config>

When I want to get this:

<config>
  <update_secs>1380</update_secs>
</config>

I tried putting XmlIgnore on the update_mins value, but that stopped it deserializing the element. Is it possible to do this, and if so how?

My full code example is:

namespace Scrap
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "<config><update_mins>23</update_mins></config>";

            XmlSerializer searializer = new XmlSerializer(typeof(Config));
            Config result = (Config)searializer.Deserialize(new StringReader(s));

            Console.WriteLine("Minutes: " + result.Minutes);
            Console.WriteLine("Seconds: " + result.Seconds);

            Debug.Assert(result.Minutes == 23);
            Debug.Assert(result.Seconds == 1380);

            StringWriter s2w = new StringWriter();
            searializer.Serialize(s2w, result);

            Console.WriteLine(s2w.ToString());

            string xmlResult = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" +
                "<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" +                
                "  <update_secs>1380</update_secs>\r\n" +
                "</config>";

            Debug.Assert(s2w.ToString() == xmlResult);

            Console.ReadKey(true);
        }
    }

    [XmlRoot(ElementName = "config")]
    public class Config
    {
        [XmlElement(ElementName = "update_mins")]        
        public int Minutes
        {            
            get
            {
                return this.Seconds / 60;
            }    
            set
            {
                this.Seconds = value * 60;
            }
        }

        [XmlElement(ElementName = "update_secs")]
        public int Seconds { get; set; }
    }
}
  • 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-21T10:11:51+00:00Added an answer on May 21, 2026 at 10:11 am

    This is still not an ideal solution, but the way I have resolved this is to add [XmlIgnore] to the Minutes property (as Aliostad and others had suggested) and then add an AllElements property like this:

        [XmlAnyElement]
        public XmlElement[] AllElements
        {
            get
            {
                XmlElement[] value = new XmlElement[0];
                return value;
            }
    
            set
            {
                if (value != null)
                {
                    foreach (XmlElement e in value)
                    {
                        switch (e.Name)
                        {
                            case "update_mins":
                                this.Minutes = Convert.ToInt32(e.InnerText);
                                break;
                        }
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class expecting a two parameters template, like this: template<template <class, class>
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have a class with two ctors. One parameterless and one with parameters. Unity
Morning. Issue: I have a class called Reports. Two constructors. One allows no parameters,
I have a class that contains two methods like these: public String getFoo(Int32 a)
I have a class split across two files. One of these is generated, the
I have a really simple class with two methods; One that will be called
Let's say I have two models, Classes and People. A Class might have one
I have two classes, and want to include a static instance of one class
I have a class with two methods defined in it. public class Routines {

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.