Solved

C# XML serialization dynamically ignore properties at runtime

Posted on 2010-09-09
4
1,633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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