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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:03:30+00:00 2026-06-15T19:03:30+00:00

I am using xml serialization, with rectangles but this is producing some nasty XML…

  • 0

I am using xml serialization, with rectangles but this is producing some nasty XML…

My class is something like this:

    [Serializable]
    public class myObject
    {
       public Rectangle Region { get; set; }

       //Some other properties and methods...
    }

and that give me this when i serialize it to XML:

    <myObject>
      <Region>
        <Location>
          <X>141</X>
          <Y>93</Y>
        </Location>
        <Size>
          <Width>137</Width>
          <Height>15</Height>
        </Size>
        <X>141</X>
        <Y>93</Y>
        <Width>137</Width>
        <Height>15</Height>
      </Region>
      ...
    </myObject>

Yuck!

I am hoping that I can either suppress the Size and Location properties on the Rectangle, or perhaps use backing variables and [XmlIgnore] to end up with something like this:

    [Serializable]
    public class myObject
    {
       [XmlElement("????")]
       public int RegionX;

       [XmlElement("????")]
       public int RegionY;

       [XmlElement("????")]
       public int RegionHeight;

       [XmlElement("????")]
       public int RegionWidth;

       [XmlIgnore]
       public Rectangle Region {get { return new Rectangle(RegionX, RegionY, RegionWidth, RegionHeight);}

       //Some other properties and methods...
    }

hopefully giving me something like:

    <myObject>
      <Region>
        <X>141</X>
        <Y>93</Y>
        <Width>137</Width>
        <Height>15</Height>
      </Region>
      ...
    </myObject>

Not so good in the code, but the XML will be edited by people so it would be good to get something which works there…

Any ides what might go in the “????”? or another way of doing this?

I would prefer not to have to implement my own version of Rectangle…

  • 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-15T19:03:31+00:00Added an answer on June 15, 2026 at 7:03 pm

    Thanks for the comments guys, i ended going down a sort of DTO route – I implemented my own Struct – XmlRectangle, holding the four int values i need decorated with [Serializable]. I added implicit conversion operators so i could use it as a Rectangle:

    [Serializable]
    public struct XmlRectangle
    {
        #endregion Public Properties
    
        public int X {get; set; }
        public int Y {get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
    
        #endregion Public Properties
    
        #region Implicit Conversion Operators
    
        public static implicit operator Rectangle(XmlRectangle xmlRectangle)
        {
            return new Rectangle(xmlRectangle.X, xmlRectangle.Y, xmlRectangle.Width, xmlRectangle.Height);
        }
    
        public static implicit operator XmlRectangle(Rectangle rectangle)
        {
            return result = new XmlRectangle(){ X = rectangle.X, Y = Rectangle.Y, Height = Rectangle.Height, width  = Rectangle.Width };
        }
    
        #endregion Implicit Conversion Operators
    }
    

    Then the class which held it as data had a Rectangle property exposing it as an XmlRectangle to the serializer the like this:

    [XmlElement("Region",typeof(XmlRectangle))]
    public Rectangle Region { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using XML serialization in C#, I use code like this: public MyObject LoadData()
I made an xml document by using XML Serialization. It looks like this <?xml
I want to deserialize back my class using System.Xml.Serialization, but i've noticed a strange
I have a class that serializes a set of objects (using XML serialization) that
I am serializing objects to XML using System.Xml.Serialization and this requires me to have
I was using the xml serializer but when I switched to binary serialization, it
I'm new into XML Serialization using .NET and after working with it for some
I've been using XML serialization for a while, and today I realized something really
I have to realize a serializazion/deserialization class and i'm using System.Xml.Serialization . I have
I want to serialize a ReportDocument using XML serialization but in vain, that's my

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.