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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:33:30+00:00 2026-05-10T13:33:30+00:00

Following on from my recent question on Large, Complex Objects as a Web Service

  • 0

Following on from my recent question on Large, Complex Objects as a Web Service Result. I have been thinking about how I can ensure all future child classes are serializable to XML.

Now, obviously I could implement the IXmlSerializable interface and then chuck a reader/writer to it but I would like to avoid that since it then means I need to instantiate a reader/writer whenever I want to do it, and 99.99% of the time I am going to be working with a string so I may just write my own.

However, to serialize to XML, I am simply decorating the class and its members with the Xml??? attributes ( XmlRoot , XmlElement etc.) and then passing it to the XmlSerializer and a StringWriter to get the string. Which is all good. I intend to put the method to return the string into a generic utility method so I don’t need to worry about type etc.

The this that concerns me is this: If I do not decorate the class(es) with the required attributes an error is not thrown until run time.

Is there any way to enforce attribute decoration? Can this be done with FxCop? (I have not used FxCop yet)

UPDATE:

Sorry for the delay in getting this close off guys, lots to do!

Definitely like the idea of using reflection to do it in a test case rather than resorting to FxCop (like to keep everything together).. Fredrik Kalseth’s answer was fantastic, thanks for including the code as it probably would have taken me a bit of digging to figure out how to do it myself!

+1 to the other guys for similar suggestions 🙂

  • 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. 2026-05-10T13:33:31+00:00Added an answer on May 10, 2026 at 1:33 pm

    I’d write a unit/integration test that verifies that any class matching some given criteria (ie subclassing X) is decorated appropriately. If you set up your build to run with tests, you can have the build fail when this test fails.

    UPDATE: You said, ‘Looks like I will just have to roll my sleeves up and make sure that the unit tests are collectively maintained’ – you don’t have to. Just write a general test class that uses reflection to find all classes that needs to be asserted. Something like this:

    [TestClass] public class When_type_inherits_MyObject {     private readonly List<Type> _types = new List<Type>();      public When_type_inherits_MyObject()     {         // lets find all types that inherit from MyObject, directly or indirectly         foreach(Type type in typeof(MyObject).Assembly.GetTypes())         {             if(type.IsClass && typeof(MyObject).IsAssignableFrom(type))             {                 _types.Add(type);             }         }     }      [TestMethod]     public void Properties_have_XmlElement_attribute     {         foreach(Type type in _types)         {             foreach(PropertyInfo property in type.GetProperties())             {                 object[] attribs = property.GetCustomAttributes(typeof(XmlElementAttribute), false);                 Assert.IsTrue(attribs.Count > 0, 'Missing XmlElementAttribute on property ' + property.Name + ' in type ' + type.FullName);             }         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class Program { static void Main(string[] args) { Timer timer… May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Use GROUP_CONCAT SELECT GROUP_CONCAT(bar) FROM TABLE GROUP BY foo; May 11, 2026 at 10:50 pm
  • Editorial Team
    Editorial Team added an answer Your code doesn't work because the function is not returning… May 11, 2026 at 10:50 pm

Related Questions

Following on from my recent question regarding parsing XML files in Java I have
Following on from my previous question, Python time to age , I have now
I want a quick an easy way to check my ip address after reading
Almost 5 years ago Joel Spolsky wrote this article, The Absolute Minimum Every Software

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.