0

I have an XSD and would like to serialise it into a C# class. My current process is

Go to https://www.liquid-technologies.com/online-xsd-to-xml-converter

to convert the XSD to XML and then convert this XML to a C# class using https://xmltocsharp.azurewebsites.net/

This works but occasionally i get the error "Unexpected node type Element" or similar errors (Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content)If i get the XML of the record i get the error from, i re-run the above process to generate the C# class again then other records return an error.

So i checked another site to see if the "non working" XML validates against the XSD - it does!

To me it seems the above URLs may not be generating the C# classes correctly. Is there another way to generate these classes? OR how should i modify the class manually, so that it meets whichever record it loads?

Computer
  • 2,149
  • 7
  • 34
  • 71
  • Have you tried this method- https://stackoverflow.com/questions/5217665/how-to-generate-net-4-0-classes-from-xsd – akg179 Sep 19 '19 at 12:08
  • Just trying this and i copied the http XSD locally and ran that command. Currently i get a range of errors such as "Schema validation warning: Type 'varchar_30' is not declared. Line 228, position 5. Schema validation warning: Type 'varchar_100' is not declared. Line 233, position 5. Schema validation warning: Type 'varchar_50' is not declared. Line 238, position 5. Schema validation warning: Type 'varchar_150' is not declared. Line 243, position 5." - Not sure if ive done something wrong? – Computer Sep 19 '19 at 12:24
  • Is your xsd too proprietary to share? If not could you post it? Not sure about the online solutions, but xsd.exe is pretty darn spot on and it sounds like your xsd is not valid. – Simon Wilson Sep 19 '19 at 12:33
  • It had external references to other XSDs. I have downloaded them and put them within the same directory (so one long class generated). Currently each call i make to retrieve an XML is parsed through a class and i have a Generic method (passing in the class name and it outputs those details) This structure looks completely different. Let me see if i can work out how to use this generated class with what i have. – Computer Sep 19 '19 at 12:43
  • Not sure if you can answer this but I have 5 linked XSDs within the main XSD. I've created the class from the main XSD. Currently it looks as if the class generated is one huge class and possibly appending the default node name against properties. The current program makes different calls to URLs i.e. GetOrganisation, then GetDept and GetEmployee etc all with different URLs. Using this class should I be able/supposed to make a call to the main URL and then parse that response into this new class allowing me to iterate through the data as required? – Computer Sep 19 '19 at 13:17

0 Answers0