Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1786
  • Last Modified:

C# XML serialization dynamically ignore properties at runtime


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
codechewer
Asked:
codechewer
1 Solution
 
MalathiSekkappanCommented:
How are you serializing?  You can check for null values and not include them.  I can help you if post code snippets.
0
 
existenz2Commented:
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
 
vusovCommented:
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
 
codechewerAuthor Commented:
it worked perfectly for me! thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now