How to load XML objects into .Net classes [modified]
-
Hi all, Hoping someone can point me in the right direction here. I'm writing a VB.Net 2.0 WinForms admin app to allow users to do maintenance on an Oracle database behind another very large app. To keep the size of the admin app down and to avoid a maintenance nightmare when the tables (about 70 of them) change I am trying to write it so it's totally automated. I already have code to read the schema of each table to get the columns and then I have a form that adds an appropriate custom control for each type of table column. This works very well but I need it to be advanced enough to hide some columns, make sure others are entered, validate some others, etc, etc. So my idea is to have an XML file (see below) containing the tables and columns which can be maintained and when the app starts it would read the file and load the data into .Net classes which correspond to the XML (see class definitions below). I can then use these table and column classes to apply custom behaviour to the custom edit form I already have. So, looking at the XML and class definitions (both kept VERY simple for illustration purposes) can anyone tell me how best to go about this? How could I best read the XML file in and convert it to the classes I've defined? TIA for any help... I know how to write using lots of fairly manual code but am hoping there is a smarter & quicker way to to do this. Mike.
Public Class SAMTables Public Tables() as SAMTAble End Class Public Class SAMTable Public TableName as String Public CustomForm as String Public Columns() as SAMColumn End Class Public
-
Hi all, Hoping someone can point me in the right direction here. I'm writing a VB.Net 2.0 WinForms admin app to allow users to do maintenance on an Oracle database behind another very large app. To keep the size of the admin app down and to avoid a maintenance nightmare when the tables (about 70 of them) change I am trying to write it so it's totally automated. I already have code to read the schema of each table to get the columns and then I have a form that adds an appropriate custom control for each type of table column. This works very well but I need it to be advanced enough to hide some columns, make sure others are entered, validate some others, etc, etc. So my idea is to have an XML file (see below) containing the tables and columns which can be maintained and when the app starts it would read the file and load the data into .Net classes which correspond to the XML (see class definitions below). I can then use these table and column classes to apply custom behaviour to the custom edit form I already have. So, looking at the XML and class definitions (both kept VERY simple for illustration purposes) can anyone tell me how best to go about this? How could I best read the XML file in and convert it to the classes I've defined? TIA for any help... I know how to write using lots of fairly manual code but am hoping there is a smarter & quicker way to to do this. Mike.
Public Class SAMTables Public Tables() as SAMTAble End Class Public Class SAMTable Public TableName as String Public CustomForm as String Public Columns() as SAMColumn End Class Public
Sounds like the
XmlSerializer
is what you're looking for.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Sounds like the
XmlSerializer
is what you're looking for.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
Thanks - what I did in the end was read the file into an XMLDataDocument object instead of a class structure as it means I can use XPath/XQuery and also have the power of the DatatSet object as well. Once I created the schema file (by loading the XML file into VS2005 and choosing Create Schema from the XML menu) the code was simply as follows:
Public Sub LoadXMLConfig() 'Read the XML configuration file dataDoc = New XmlDataDocument() dataDoc.DataSet.ReadXml(My.Settings.XMLConfigFile) End Sub
Just thought I'd post this in case anyone else is wanting to do this sort of thing and comes across this thread. Mike