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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:54:01+00:00 2026-05-26T09:54:01+00:00

I am trying to serialize an object but I am facing some issues regarding

  • 0

I am trying to serialize an object but I am facing some issues regarding the attributes of a parent element that contains an array.

I have the following xml structure and I can’t add the attribute in RatePlans element.

<Root>
<RatePlans Attribute="??this one??">
    <RatePlan Attribute1="RPC" Attribute2="MC" Attribute3="RPT">
        .
        .
        .
    </RatePlan>
    <RatePlan Attribute1="RPC2" Attribute2="MC3" Attribute3="RPT4">
        .
        .
        .
    </RatePlan>
</RatePlans>
</Root>

This is what I have done so far:

namespace XmlT {
    [Serializable]
    [XmlRoot("Root")]
    public class Root {
        public List<RatePlan> RatePlans { get; set; }
    }
}

namespace XmlT {
[Serializable]
public class RatePlan {


    [XmlAttribute]
    public string RatePlanCode { get; set; }

    [XmlAttribute]
    public string MarketCode { get; set; }

    [XmlAttribute]
    public string RatePlanType { get; set; }
}
}

This gives me a correct structure but I don’t know how to add the attribute I want

Another approach

I’ve tried also another approach but this gives me wrong values at all.

namespace XmlT {
    [Serializable]
    [XmlRoot("Root")]
    public class Root {
        public RatePlans RatePlans { get; set; }
    }
}

namespace XmlT {
[Serializable]
public class RatePlans {

    [XmlAttribute]
    public string HotelCode { get; set; }

    public List<RatePlan> RatePlan { get; set; }
}
}

EDIT

this the method that I am using for the serialization

protected static string Serialize<T>(object objToXml, bool IncludeNameSpace = false) where T : class {
        StreamWriter stWriter = null;
        XmlSerializer xmlSerializer;
        string buffer;
        try {
            xmlSerializer = new XmlSerializer(typeof(T));
            MemoryStream memStream = new MemoryStream();
            stWriter = new StreamWriter(memStream);
            if (!IncludeNameSpace) {

                var xs = new XmlSerializerNamespaces();

                xs.Add("", "");
                xmlSerializer.Serialize(stWriter, objToXml, xs);
            } else {
                xmlSerializer.Serialize(stWriter, objToXml);
            }
            buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
        } catch (Exception Ex) {
            throw Ex;
        } finally {
            if (stWriter != null) stWriter.Close();
        }
        return buffer;
    }

Does anyone know how could I do this?

Thanks

  • 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-26T09:54:01+00:00Added an answer on May 26, 2026 at 9:54 am

    If the RatePlans class from the 2nd example inherits from List<RatePlan> you will get the desired result:

    [Serializable]
    public class RatePlans: List<RatePlan>
    {
        [XmlAttribute]
        public string HotelCode { get; set; }
    }
    

    Edit:

    My bad. Fields of classes inheriting from collections will not be serialized. I didn’t knew that. Sorry…

    However, this solution works:

    [Serializable]
    [XmlRoot("Root")]
    public class Root 
    {
        public RatePlans RatePlans { get; set; }
    }
    
    
    [Serializable]
    public class RatePlans
    {
        [XmlAttribute]
        public string HotelCode { get; set; }
    
        [XmlElement("RatePlan")]
        public List<RatePlan> Items = new List<RatePlan>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I'm trying to serialize into an object. Some
I'm trying to serialize an object to XML that has a number of properties,
I've run into some problems when trying to serialize my object to XML. The
I have been trying to serialize a pretty big object. This object uses dictionaries
I'm trying to figure out a way to serialize some Django model object to
i am trying to XML serialize a class object but having the following problem:
I'm trying to serialize objects from a database that have been retrieved with Hibernate,
I'm trying to serialize a bool object using in the element text and I'm
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
I'm trying to serialize some data to xml in a way that can be

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.