0

I use VB.net and the IXmlSerializable-interface for a some class with "difficult" xml-element. I made a basic class where I put all the magic and all xml-relevant class with some "difficult" xml-element inherited form these class.

During the "WriteXml"-method I do the magic for the difficult elements. (this works) And I also want to do "normal" staff for the "normal" xml-elements/-attribute. For that, i get over reflection all the properties with the right attributes. Simple attribute and element is no problem (WriteElementString(),..), but is the "normal" element a class and has xml-sub-elements, then I get a problem. How can I realize a normal serialization of xml-element in a WriteXml-method (in a basic class).

The real question is: How can I write in the XmlWrite-method a subelement (which has no ixmlserilization) of an actual element like the natural way. (these subelement have also subelements!)

Simple example:

<example>
 <simpleelement>123</simpleelement>
 <difficultelement>...magic </difficultelement>
 <problemelement>
  <simpleelement>123</simpleelement>
  <simpleelement>123</simpleelement>
 </problemelement>
</example>

Cit
  • 113
  • 1
  • 10
  • If a property is a simple text and contains children then you need a class for these descendants. – jdweng Apr 17 '20 at 15:01
  • Please focus on my last question, that is my problem. How can I handle "normal" subelements (which has also "normal" subelements, etc.) in the XmlWrite-methode of an "difficult" element. I look for something like "writer.WriteElement(...)", which serialize automatic all subelement like the normal way, because the subelements has no "IXmlSerializable". The only thing I found was "write.WriteElementString(name,value)". But this method do not cares about the subelements! – Cit Apr 17 '20 at 15:52
  • You have to write Start Element, then write children, and finally write end element. – jdweng Apr 17 '20 at 16:00
  • You can construct and use a nested `XmlSerializer` from within `ReadXml()` and `WriteXml()` as shown in, e.g., the answers to [Deserialize Comments using Custom IXmlSerializer](https://stackoverflow.com/q/61190548/3744182) and [Why does XmlSerializer throws an Exception and raise a ValidationEvent when a schema validation error occurs inside IXmlSerializable.ReadXml()](https://stackoverflow.com/q/60449088/3744182). – dbc Sep 28 '20 at 01:05

0 Answers0