Solved

C# XML serialization dynamically ignore properties at runtime

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

690 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