0

My boss have a strange request, he wants me to add a new function to serialize and deserialize all our products classes and add in the XML file all their property's types automatically. I can't modify the classes to add new "types properties" before every "real property".

Is there a way to do this with [XmlAttributes] or something else ?

Thank you.

Alexus
  • 276
  • 1
  • 5
  • 22
  • I think he wants you to create a schema for the xml file so types can automatically be checked. – jdweng Jul 13 '20 at 09:52
  • He wants to add manually informations in the xml file (result of serialisation) and the program should deserialise the edited xml file and detects that a new property was added and its type.... without any definition of the deserialisation classes... its SYFY to me – Alexus Jul 13 '20 at 12:39
  • How are you suppose to get the type without a schema? An xml file should have a version of the xml so you can tell when the version was changed. Also should have a schema so you can create new c# classes from the schema. You can create a comparison tool the check when tags have changed but you do not want to automatically modify any source code automatically without a thorough review of the differences. – jdweng Jul 13 '20 at 13:08
  • There are several different serializers. Which one do you use? – Alexander Petrov Jul 13 '20 at 13:28
  • I use XmlSerializer – Alexus Jul 13 '20 at 13:32
  • You can create a custom XmlWriter that will add additional information on the fly. – Alexander Petrov Jul 13 '20 at 13:41
  • ok good idea, but he wants to edit manually the xml files (to add a new property for example), and need a magical program that would detect this modification and could deserialise automatically the edited file. – Alexus Jul 13 '20 at 13:46
  • :-) :-) :-) :-) – Alexus Jul 13 '20 at 14:11
  • Provide more info. Current xml representation and desired result. C# code of class. – Alexander Petrov Jul 13 '20 at 14:57

1 Answers1

-1

Maybe this is what you are looking for https://learn.microsoft.com/dotnet/api/system.xml.serialization.xmltypeattribute?view=netcore-3.1

this article describes the XmlTypeAttribute, which you can use to add a type attribute (in xml) on your properties tag.

[XmlType("aType")]
public string MyProperty {get;set;}
ddfra
  • 2,413
  • 14
  • 24
  • _[Your answer is in another castle: when is an answer not an answer?](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer)_. Consider editing your answer to contain a summary of the article pointed to by the link. –  Jul 13 '20 at 10:08