How to serialize only some properties in .Net?

This is for a web project, so I have several classes that inherit from Web.UI.

I only want to serialize very specific properties (basically, only local properties)

I know the XMLIgnore property, which can be placed in the property to ignore elements, but this will not work in my context, since this will require modifying a bunch of things that I really don't want to change (and probably can't).

So, how can I tell the xml serializer to ignore everything except X and Y, or tell it to serialize only X and Y?

I could just create my own xml in a string builder or something else, and if that is the only way, let it be. however, I am looking for a method that will use the built-in XML material.

thanks

+4
source share
3 answers

For custom serialization, you can get your class from the ISerializable Interface and provide custom serialization accordingly

+2
source

Why don't you implement IXmlSerializable ?

+2
source

You can put some stuff into it and just rip the material out of XML after it is returned.

This is a terrible idea compared to XMLIgnore, which is the right solution, but you can certainly pull up the XML, scroll through it and delete everything that you don't want to see.

Sometimes terrible ideas are the best.

0
source

Source: https://habr.com/ru/post/1304210/


All Articles