Solved

C# XML serialization dynamically ignore properties at runtime

Posted on 2010-09-09
4
1,579 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

26 Experts available now in Live!

Get 1:1 Help Now