0

I have a huge number of XML files and I need to convert them to C# classes so I can later deserialize them. I need to automate the XML files to C# class file conversion.

Note: The XML files are somewhat similar in structure but some introduce other attributes.

I have tried the following:

1. XSD

By using XSD included with visual studio, the whole process can be easily automated using C#. By making an XSD for each file and turning them to .cs this is possible. The problem is that the complexity of the XSD files raise problems and the conversion won't be correct. Deserialisation won't work due to the following error:

Cannot convert type 'AttributeName[]' to 'AttributeName'

This error is present for every attribute generated by the .xsd.

I have also tried generating an XSD for all the XML files and converting the XSD into C#. The deserialisation still doesn't work, this time marking all attributes as null.

2. Paste as Special (XML to C#)

Visual Studio offers quite a nifty feature and the deserialisation works with this. Yet I have no idea how I can automate this for every file.

3. XML2CSharp.com

This works too, but the problem remains the automation. I have quite a lot of files and I cannot do such process over and over again.

An ideal solution would be a convertToObject() function that makes a new C# object for the current XML file being accessed. I'm not sure how maybe I could automate the second method that I tried.

drew181
  • 333
  • 1
  • 10
  • Are you looking to create _objects_ out of the XML files, or classes? What do the files look like in general? – AKX Jul 18 '19 at 11:35
  • I am pretty sure you don't understand the concept of using an `XML`. You would create a `C#` class object only if the `XML` as a static format and that is only done once by hand and that's it. If you need variable formatting then you don't need a `.cs` class at all. You just need to use the XML serializer and query the nodes directly from that object. – Franck Jul 18 '19 at 11:37
  • Ahhh I meant classes, apologies for the confusion – drew181 Jul 18 '19 at 11:38
  • Download latest XSD.EXE from msdn. Your executable may have errors. – jdweng Jul 18 '19 at 11:40
  • I have the latest XSD installed – drew181 Jul 18 '19 at 11:43
  • If these files have no fixed schema, you may just need to load them using LINQ to XML and `XDocument.Load()`. See: [How does one parse XML files?](https://stackoverflow.com/a/55829/3744182). Alternatively if they share common schema elements you could create a basic c# data model for the common properties and capture the remainder with `XmlAnyElement` and `XmlAnyAttribute`. See: [XmlSerializer equivalent of IExtensibleDataObject](https://stackoverflow.com/a/2511991/3744182) and possibly [XmlSerializer - node containing text + xml + text](https://stackoverflow.com/q/25995609). – dbc Jul 18 '19 at 19:25
  • But beyond that general advice it's hard to see how we could answer the question without some concrete examples -- i.e. a [mcve]. – dbc Jul 18 '19 at 19:26

0 Answers0