Click here to Skip to main content
15,891,372 members
Articles / Programming Languages / Visual Basic
Article

Xml to VB.net Class Generator

Rate me:
Please Sign up or sign in to vote.
1.72/5 (7 votes)
3 Jul 20061 min read 65.7K   3.5K   26   8
Converts XML Document to VB.Net Class

Introduction

Sample screenshot

XML2Class Generator converts the XML Document to VB.Net Class. The reason behind developing this application was I have some components those returns xml string. I want to expose them out as strongly typed classes. I tried to implement Dataset by creating schema from xml and generating Dataset, but for the two reasons I left that idea.

Reason 1. Some of the tags in the xml documents have attributes and the dataset was created with child tables for each tag that has the attributes. I don’t like that approach.

Reason 2. I felt Dataset is too heavy to do this kind of work and it has lot of functionality that is not useful in this scenario.


Then I tought i would write a class manually which reads the xml document and copy the values to the class properties. But I felt it is laborious job, since the xml document has so many tags. I left this idea too.


Hence I decided to write XML to Class generator.

 

Features:

1. Generates class from xml. (This version only supports XML file with one root node )

2. Handles the xml attributes by converting them as separate properties. The xml attribute will be rendered as a property with this format “NodeName_Attributename”.

Example : <Name First=”Srinivas”/>

Attribute First will be created as property Name_First.

3.Can Be serialized from the xml.

I have included a sample project which has the logic to create an instance of the above generated class by deserializing from the xml.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDid not work on more complicated XML.... Pin
Craig P Williams Sr20-Jul-17 7:28
professionalCraig P Williams Sr20-Jul-17 7:28 
General[My vote of 2] xsd Pin
Member 904093029-May-12 2:32
Member 904093029-May-12 2:32 
GeneralMy vote of 5 Pin
somethingfree12318-Jan-11 5:28
somethingfree12318-Jan-11 5:28 
GeneralMy vote of 1 Pin
Dave Kreskowiak7-Mar-10 15:44
mveDave Kreskowiak7-Mar-10 15:44 
GeneralCouple things... [modified] Pin
BoneSoft3-Jul-06 9:40
BoneSoft3-Jul-06 9:40 
First, your download link is broken.

Second, is a question, that would likely be answered from viewing the code. It looks like you are using custom attributes to represent XML mappings (SourceXMLTag), do you have a custom XML Serializer for this?

Have you looked at using the Framework's XmlSerializer? From the problem that you describes as your primary motivation for this, I would think that XSD.exe or something similar would solve your problem. Though I understand your interest in producing a custom solution. I did something similar to this (XmlModeler[^]). Mostly out of disgust for the crap code that XSD.exe produces, and it's lack of language options and code customization options.

I'll be watching for the download link to be fixed so I can see your code.



Try code model generation tools at BoneSoft.com.

-- modified at 12:03 Wednesday 5th July, 2006

Ok, after viewing the code it looks like you are reinventing the wheel with a different approach to XML serialization. Which is much less robust than the Frameworks XmlSerializer. Besides that, you don't have a library to hold the classes that would need to be reused for this, your test project has the custom attribute and base object. This implementation doesn't work with some XML constructs like namespaces. Plus you're flatening everything to strings and flatening the entire structure to a single class.

You should look at the XmlSerializer class for doing this kind of work. There's really very little involved in using the XmlSerializer, and it's much more robust. The only downside to it is that you need to hand code classes to mirror your XML structure, and there are tools to do that for you such as XSD.exe and XmlModeler[^].

This may still be an idea worth persuing, but I think it still needs lots of work.
GeneralRe: Couple things... Pin
zhaojicheng18-Feb-09 2:00
zhaojicheng18-Feb-09 2:00 
GeneralRe: Couple things... Pin
BoneSoft18-Feb-09 2:20
BoneSoft18-Feb-09 2:20 
GeneralRe: Couple things... Pin
zhaojicheng18-Feb-09 4:21
zhaojicheng18-Feb-09 4:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.