XML read - code in module

ManicCW

Well-known member
Joined
Jul 21, 2005
Messages
428
Location
Mostar
Programming Experience
Beginner
hi,
im developing app that reads data from XML and then puts that data in controls on form. Also this data can be again saved in XML. Now I need to know which way is the best before I continue my work.

When user selects type of document (XML) to load my app loads usercontrol in main form and populates controls. There are many different usercontrols for every type of document (example: contact, drivers licence, car info ...). I used this code just to test loading:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] FileStream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Stream
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] AppDirectory [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StartPath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = AppDirectory & [/SIZE][SIZE=2][COLOR=#800000]"\"[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.InitialFileDirecorty
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Directory.Exists(StartPath) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Directory.CreateDirectory(StartPath)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OpenXMLFile.Filter = [/SIZE][SIZE=2][COLOR=#800000]"XML dokumenti (*.xml)|*.xml"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OpenXMLFile.InitialDirectory = StartPath
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OpenXMLFile.DefaultExt = [/SIZE][SIZE=2][COLOR=#800000]"xml"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OpenXMLFile.RestoreDirectory = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] OpenXMLFile.ShowDialog() = Windows.Forms.DialogResult.OK [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]FileStream = OpenXMLFile.OpenFile()
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] (FileStream [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xread [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlTextReader = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlTextReader(FileStream)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xdoc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] XmlDocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] XmlDocument
xdoc.Load(xread)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].pnLoader.Controls.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.DocumentStatus = [/SIZE][SIZE=2][COLOR=#800000]"Modify"
[/COLOR][/SIZE][SIZE=2][/SIZE][B][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NMIL [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/B][SIZE=2][B] MIL[/B]
[/SIZE][SIZE=2][COLOR=#0000ff][B]Me[/B][/COLOR][/SIZE][SIZE=2][B].pnLoader.Controls.Add(NMIL)[/B]
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].pnLoader.Controls(0).Dock = DockStyle.Fill
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ctr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] [B]NMIL[/B].Controls(0).Controls
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]TypeOf[/COLOR][/SIZE][SIZE=2] ctr [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] TextBox [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](ctr, TextBox).Text = xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]TypeOf[/COLOR][/SIZE][SIZE=2] ctr [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] DateTimePicker [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](ctr, DateTimePicker).Value = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2](xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]FileStream.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

Bolded part is where I load usercontrol. MIL is class name of usercontrol. Now my questions:

  1. how can I make bolded part to be dynamic for all usercontrols - how can i put it in module?
  2. is this good way to read files or should i load it in dataset and bind controls to dataset (help on this would be great)?
  3. should i use usercontrols or mdi form and child forms?
btw: here is example XML file:

VB.NET:
[SIZE=2][COLOR=#0000ff]<?[/COLOR][/SIZE][SIZE=2][COLOR=#800000]xml[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] [/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]version[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]1.0[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff] [/COLOR][/SIZE][SIZE=2][COLOR=#ff0000]encoding[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff]utf-8[/COLOR][/SIZE][SIZE=2]"[/SIZE][SIZE=2][COLOR=#0000ff] ?>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]CRODocument[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]DocumentType[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE][SIZE=2]Naljepnica[/SIZE][SIZE=2][COLOR=#0000ff]</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]DocumentType[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]DocumentCategory[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE][SIZE=2]MIL[/SIZE][SIZE=2][COLOR=#0000ff]</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]DocumentCategory[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]Stetnik[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE][SIZE=2]Jadranko Dragoje[/SIZE][SIZE=2][COLOR=#0000ff]</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]Stetnik[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]StetnikAdresa[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE][SIZE=2]Stjepana Radi†a 40[/SIZE][SIZE=2][COLOR=#0000ff]</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]StetnikAdresa[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]StetnikRegistracija[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE][SIZE=2]123-M-456[/SIZE][SIZE=2][COLOR=#0000ff]</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]StetnikRegistracija[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
<[/COLOR][/SIZE][SIZE=2][COLOR=#800000]StetnikVozilo[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>[/COLOR][/SIZE][SIZE=2]FORD Mondeo 1.8[/SIZE][SIZE=2][COLOR=#0000ff]</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]StetnikVozilo[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
</[/COLOR][/SIZE][SIZE=2][COLOR=#800000]CRODocument[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]>
[/COLOR][/SIZE]
 
Back
Top