Solved

C# XML serialization dynamically ignore properties at runtime

Posted on 2010-09-09
4
1,558 Views
Last Modified: 2012-05-10

i am trying to serialize a class but i need to serialize only properties that have a value in it. if the property has no value, then  i don't want to serialize it with an empty element tag.

is there a way to do this in C#?
0
Comment
Question by:codechewer
4 Comments
 
LVL 1

Expert Comment

by:MalathiSekkappan
ID: 33642819
How are you serializing?  You can check for null values and not include them.  I can help you if post code snippets.
0
 
LVL 14

Expert Comment

by:existenz2
ID: 33644241
It might be easier to just fully serialize the class and then after serializing validate which nodes contain no data and remove those from the XML. That can be done without rewriting the serializers etc..

Another scenario would be to create your own serializer, but the first option is much easier in my opinion.
0
 
LVL 3

Accepted Solution

by:
vusov earned 125 total points
ID: 33646127
If you need to serialize only some properties dynamically, you should add property: 
public bool YourPropertyNameSpecified { get; set; }
to you class and  then set  YourPropertyNameSpecified = true if you want to serialize  YourPropertyName property. I think this will help you.
0
 

Author Closing Comment

by:codechewer
ID: 33649452
it worked perfectly for me! thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now