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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:21:04+00:00 2026-06-11T14:21:04+00:00

DataContract and DataMember attributes may be also used in order to serialize an object

  • 0

DataContract and DataMember attributes may be also used in order to serialize an object to file and for deserialization, by using a DataContractSerializer. Suppose we have a class with the following private field and public property.

public class MyClass
{
    private int positiveValue;

    public int PositiveValue
    {
        get { return positiveValue; }
        set
        {
            if (value < 1)
                throw new ArgumentOutOfBoundException(...);
            positiveValue = value;
        }
    }
}

Now suppose we have an XML file containing the state of a previously serialized object, and assume that the user has modified this file, specifying an incorrect value (that is a non positive value) for the PositiveValue property. During deserialization an exception would be thrown, since the value in the file is invalid.

Suppose we want to deserialize a list of MyClass objects from a file: if some object is not valid, an exception is thrown. Is it possible to make sure that the DataContractSerializer ignores invalid objects? Moreover, taking into account the problem just explained, is good practice to throw exceptions in a property with DataMember attribute applied?

  • 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-11T14:21:05+00:00Added an answer on June 11, 2026 at 2:21 pm

    In terms of a list, no you can’t make DCS ignore invalid objects. If an exception occurs, the entire deserialize is aborted. If it doesn’t throw, it it going to be added to your list.

    I would consider checking validity after-and-separate-from deserialization. In some cases, you might be able to just say “ignore invalid items in lists”, but frankly in most cases if there is any problem you just want to reject the entire thing.

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

Sidebar

Related Questions

I'm using DataContractSerializer to serialize a class with DataContract and DataMember attributes to an
In WCF you can define a contract using the [DataContract] and [DataMember] attributes, like
On my WPF application, I am using DataContractSerializer to serialize object. I observed that
[DataContract] public class Foo { [DataMember(Order = 1)] public int FooId { get; set;
I've been using WCF's DataContract and DataContractSerializer to read/write objects to XML files. We
I have a class that is marked with DataContract attributes and I would like
I'm trying to serialize a class derived from List<> using DataContract. The problem is
I am using .NET 3.5SP1 and DataContractSerializer to serialize a class. In SP1, they
I have a wcf method that accepts an object Order as its input and
I have a a datacontract which has important attributes on. For example, in the

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.