0

Is it possible to serialize a C# object to an XML file with this special format?

class Job
{
    Guid         JobGuid;
    List<Source> Sources; 
}

I want to get this XML format:
Module_n (with n the index of the sub-element) instead of Source

<job JobGuid="....."> 
  <Sources>
    <Module_1> </Module_1>
    <Module_2> </Module_2>
  </Sources>
</job>
zx485
  • 28,498
  • 28
  • 50
  • 59
  • But not easy. For `jobGuid` just add `[XmlAttribute("JobGuid")]` to the `JobGuid` property. But getting the required list element names means you will have to implement `IXmlSerializable`, which is quite burdensome. To get started see [How do you deserialize XML with dynamic element names?](https://stackoverflow.com/q/37255149/3744182) and [how to derive xml element name from an attribute value of a class using annotations?](http://stackoverflow.com/a/28770138/3744182). But this XML is poorly designed since it cannot have a proper XSD. Do you have freedom to change the format? – dbc Mar 18 '17 at 16:49
  • Also, make the class `Job` and the properties `JobGuid` and `Sources` public. – dbc Mar 18 '17 at 16:50
  • Unfortunately, i have to respect this format. I would like to build a API that do the same thing as the original API. – EL Hirach Abderrazzak Mar 20 '17 at 08:44

0 Answers0