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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:43:06+00:00 2026-05-26T02:43:06+00:00

I want to specify that one property in an XML serializable class is an

  • 0

I want to specify that one property in an XML serializable class is an attribute of another property in the class, not of the class itself. Is this possible without creating additional classes?

For example, if I have the following C# class

class Alerts
{
  [XmlElement("AlertOne")]
  public int AlertOneParameter { get; set; }

  public bool IsAlertOneEnabled { get; set; }
}

how can I specify that IsAlertOneEnabled is an attribute of AlertOne so that the XML serializes to the following?

<Alerts>
  <AlertOne Enabled="True">99</AlertOne>
</Alerts>
  • 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-26T02:43:07+00:00Added an answer on May 26, 2026 at 2:43 am

    If you are using XmlSerializer with default (non-IXmlSerializable) serialization, then indeed: this cannot be achieved without adding an extra class that is the AlertOne, with an attribute and a [XmlText] value.

    If you implement IXmlSerializable it should be possible, but that is not a nice interface to implement robustly (the deserialization, in particular, is hard; if it is write-only then this should be fine). Personally I’d recommend mapping to a DTO model with the aforementioned extra class.

    Other tools like LINQ-to-XML would make it pretty simple, of course, but work differently.

    An example of a suitable DTO layout:

    public class Alerts
    {
        [XmlElement("AlertOne")]
        public Alert AlertOne { get; set; }
    }
    public class Alert
    {
        [XmlText]
        public int Parameter { get; set; }
        [XmlAttribute("Enabled")]
        public bool Enabled { get; set; }
    }
    

    You could of course add a few [XmlIgnore] pass-thru members that talk to the inner instance.

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

Sidebar

Related Questions

I want to let users specify a date that may or may not include
I want to specify an argument in an attribute, like this: [OutputCache(Duration = GlobalSettings.GlobalVar)]
I want to specify that either fieldname or freetext must always be present in
I have a Java WebStart application for which I want to specify that the
I have a base class in which I want to specify the methods a
I have a calculated property in my object that I don't want to save
I´ve read that it´s possible to share sockets between processes. Is this also possible
With the svn:ignore property, is there a way I can specify what I want
this is a follow up question from this one , I don't want to
My question is about properly binding the property of one element to another. Here

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.