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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:56:03+00:00 2026-06-06T21:56:03+00:00

I have the following class: [Serializable] public class SomeModel { [XmlElement(SomeStringElementName)] public string SomeString

  • 0

I have the following class:

[Serializable]
public class SomeModel
{
    [XmlElement("SomeStringElementName")]
    public string SomeString { get; set; }

    [XmlElement("SomeInfoElementName")]
    public int SomeInfo { get; set; }
}

Which (when populated with some test data) and Serialized using XmlSerializer.Serialize() results in the following XML:

<SomeModel>
  <SomeStringElementName>testData</SomeStringElementName>
  <SomeInfoElementName>5</SomeInfoElementName>
</SomeModel>

What I need to have is:

<SomeModel>
  <SomeStringElementName Value="testData" />
  <SomeInfoElementName Value="5" />
</SomeModel>

Is there a way to specify this as attributes without writing my own custom serialization code?

  • 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-06-06T21:56:05+00:00Added an answer on June 6, 2026 at 9:56 pm

    You will need wrapper classes:

    public class SomeIntInfo
    {
        [XmlAttribute]
        public int Value { get; set; }
    }
    
    public class SomeStringInfo
    {
        [XmlAttribute]
        public string Value { get; set; }
    }
    
    public class SomeModel
    {
        [XmlElement("SomeStringElementName")]
        public SomeStringInfo SomeString { get; set; }
    
        [XmlElement("SomeInfoElementName")]
        public SomeIntInfo SomeInfo { get; set; }
    }
    

    or a more generic approach if you prefer:

    public class SomeInfo<T>
    {
        [XmlAttribute]
        public T Value { get; set; }
    }
    
    public class SomeModel
    {
        [XmlElement("SomeStringElementName")]
        public SomeInfo<string> SomeString { get; set; }
    
        [XmlElement("SomeInfoElementName")]
        public SomeInfo<int> SomeInfo { get; set; }
    }
    

    And then:

    class Program
    {
        static void Main()
        {
            var model = new SomeModel
            {
                SomeString = new SomeInfo<string> { Value = "testData" },
                SomeInfo = new SomeInfo<int> { Value = 5 }
            };
            var serializer = new XmlSerializer(model.GetType());
            serializer.Serialize(Console.Out, model);
        }
    }
    

    will produce:

    <?xml version="1.0" encoding="ibm850"?>
    <SomeModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <SomeStringElementName Value="testData" />
      <SomeInfoElementName Value="5" />
    </SomeModel>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following types [Serializable, XmlType(Namespace=http://mycompany/foo] public sealed class Limit { [XmlElement(ElementName=Value1)] public
I have the following object: [Serializable] public class ExampleImage { public int ID {
We have the followig code: [Serializable] public class Class1 { [XmlElement(description)] public string Description
Have the following structure [Serializable] public class Parent { public int x = 5;
I have the following class class UserAccount implements Serializable { public String username; public
Suppose I have the following 2 entities: @Entity public class Person implements Serializable {
I have the following mapping: @Entity @Table(name = Prequalifications) public class Prequalification implements Serializable
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have the following classes [Serializable()] public class ColumnList { public ColumnList() { ListOfColumns
I have an Abstract class: [Serializable] public abstract class BaseModel { public virtual int

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.