user1027640
user1027640

Reputation: 11

Generate class from XML

At the start I want to confirm that I'm newbie in C#. I just have some experiance in AVR ANSI C programming.

I have a exercise to make (deadline: april 2012). It's not my homework, it's only way to show my friend and teacher that I can make it. Let's present exercise idea:

Title: Class generator WinForm Detail: I need to create an application (based on System.Reflection) which will create source code (class) in c#. This application will let user to add random XML file and convert it into c# class. At the end user will be able to save it somewhere.

I have already made part of code (in MS Visual Studio C#) which let user to choose and save file.

I think it should work like that: Example of XML (I know these example could be written wrong - but i dont know XML)

<pizza>
  <vezuvio>
     <prize> 10 </prize>
  </vezuvio>
</pizza>

Converter into c#

class Pizza{
  contruct vesuvio{
    int prize = 10;
  }
} 

What help I need from you users:

  1. Helpful links about the same project/application - I was searching on that site, but couldn't found something interesting.
  2. Any ideas or poor source code to show mainly how should it work.
  3. What de-serialization is and is it going to be useful?

Upvotes: 1

Views: 3522

Answers (4)

Josh
Josh

Reputation: 13828

The best tool yet to do this job well is SimpleXmlToCode

It works well to do this job. It does not require any XSD or anything. Straight XML to POCO classes.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064184

Writing a general purpose mapping from xml to C# is ... complex, and may indeed not be possible in many cases. However, as shown by tools like xsd it is possible to infer a schema from xml. However, as a much simpler option, you could just look at parsing the xml with xslt, which can simply check for like-siblings (to decide if something is individual or a list, etc). IF you knew c#, xml and xslt well, knocking out something basic but functioning would be a pretty simple thing to do.

The complexity also depends on whether the expectation is that you should be able to populate the object model with arbitrary data from the same schema: if you can simply plug into XmlSerializer, then great! but writing a full xml serialization engine either from scratch or from XmlReader (as a starting point) is non-trivial.

Xml is also probably a lot more complex than you expect - there are lots of subtleties (not least, xmlns namespaces).

Upvotes: 3

Vikram Shetty
Vikram Shetty

Reputation: 757

1) you wont get a direct link to whar you need. Search for xml node reading and xml attribute and node name . Xquery path. 2) if you take incremental approach of finding root node first and than write it to source file and so on 3) i dont think xml searlization wont help you much

Upvotes: 0

harriyott
harriyott

Reputation: 10645

As a starting point, .NET ships with Xsd.exe, which can generate a C# class from XML / XSD. Once you've got the class, you can then Deserialize from an XML file to read the data from the file into the object's properties.

Upvotes: 0

Related Questions