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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:43:34+00:00 2026-06-05T21:43:34+00:00

I have been assigned to implement an interface to an API that uses XML

  • 0

I have been assigned to implement an interface to an API that uses XML request/response.
The API providers don’t provide any xsd(s) for the XML calls.

I generated the C# classes using xsd.exe: .xml -> .xsd -> .cs
However, I did not find the generated classes satisfactory, as the calls include a lot of lists, which xsd.exe doesn’t handle properly.

Should I take the pain and create classes manually that maps to all the request/responses? That might help in maintaining the code easily later on.
Or should I just use Xml classes given by .Net, and write methods to create the XML request/responses? That will take lesser time, but may become tough in the maintenance phase.

Here is a sample class that I have created for a corresponding XML element:

XML Element

<Product ID="41172" Description="2 Pers. With Breakfast" NonRefundable="YES" StartDate="2010-01-01" EndDate="2010-06-30" Rate="250.00" Minstay="1" />

The corresponding class

internal class ProductElement : IElement
{
    private const string ElementName = "Product";

    private const string IdAttribute = "ID";
    private const string DescriptionAttribute = "Description";
    private const string NonRefundableAttribute = "NonRefundable";
    private const string StartDateAttribute = "StartDate";
    private const string EndDateAttribute = "EndDate";
    private const string RateAttribute = "Rate";
    private const string MinStayAttribute = "Minstay";

    private string Id { get; private set; }
    internal string Description { get; private set; }
    internal bool IsNonRefundable { get; private set; }

    private DateRange _dateRange;
    private string ParseFormat = "yyyy-MM-dd";
    private decimal? _rate;
    private int? _minStay;

    internal ProductElement(string id, DateRange dateRange, decimal? rate, int? minStay)
    {
        this.Id = id;
        this._dateRange = dateRange;
        this._rate = rate;
        this._minStay = minStay;
    }
    internal ProductElement(XElement element)
    {
        this.Id = element.Attribute(IdAttribute).Value;
        this.Description = element.Attribute(DescriptionAttribute).Value;
        this.IsNonRefundable = element.Attribute(NonRefundableAttribute).Value.IsEqual("yes") ? true : false;
    }

    public XElement ToXElement()
    {
        var element = new XElement(ElementName);
        element.SetAttributeValue(IdAttribute, _id);
        element.SetAttributeValue(StartDateAttribute, _dateRange.Start.ToString(ParseFormat, CultureInfo.InvariantCulture));
        element.SetAttributeValue(EndDateAttribute, _dateRange.End.ToString(ParseFormat, CultureInfo.InvariantCulture));
        element.SetAttributeValue(RateAttribute, decimal.Round(_rate.Value, 2).ToString());
        element.SetAttributeValue(MinStayAttribute, _minStay.Value.ToString());

        return element;
    }
}

At times, I think I am taking too much pain. Sometimes, I think the pain is worth taking.
What is your opinion, people?
Also, any improvements in my class design?

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

    You’re really over thinking the problem… you can use the System.Xml.Serialization namespace to really save you time and do most of the work for you.

    Use this instead:

    public class Product
    {
        [XmlAttribute()]
        public long Id { get; set; }
        [XmlAttribute()]
        public string Description { get; set; }
        [XmlAttribute()]
        public string NonRefundable { get; set; }
        [XmlAttribute()]
        public string StartDate { get; set; }
        [XmlAttribute()]
        public string EndDate { get; set; }
        [XmlAttribute()]
        public decimal Rate { get; set; }
        [XmlAttribute()]
        public bool Minstay { get; set; }
    }
    

    And the code to test:

    class Program
    {
        static void Main(string[] args)
        {
            string xml = "<Product ID=\"41172\" Description=\"2 Pers. With Breakfast\" NonRefundable=\"YES\" StartDate=\"2010-01-01\" EndDate=\"2010-06-30\" Rate=\"250.00\" Minstay=\"1\" />";
            XmlSerializer ser = new XmlSerializer(typeof(Product));
    
            using(MemoryStream memStream = new MemoryStream())
            {
                byte[] data = Encoding.Default.GetBytes(xml);
                memStream.Write(data, 0, data.Length);
                memStream.Position = 0;
                Product item = ser.Deserialize(memStream) as Product;
                Console.WriteLine(item.Description);
            }
        }
    }
    

    One final note, you will notice I didn’t really bother doing anything overly fancy with the conversion for dates and such, but you can easily expand upon this for the additional details. The main thing you should take away from this is that you’re really over-thinking this whole thing.

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

Sidebar

Related Questions

I have been assigned wit the task to write a program that takes a
I'd like to create a procedure that selects all records that have been assigned
I have recently been assigned a CSS & design project that's in a CakePHP
I have been assigned a project to develop a set of classes that act
I have been assigned to create a secure server-client access lock, so that server
I have been assigned to create a XSD schema for a proposed XML. i
I have been assigned to Convert a VB.NET project to C# and I got
In fact I have been assigned with a task to secure my DLL, so
I need some help with a work project I have been assigned. At the
I have been assigned the task of converting an SQL Server Database to 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.