Solved

Make xml file in vb.net to populate it...

Posted on 2014-10-21
2
243 Views
Last Modified: 2014-10-21
Question: How can I code in vb.net to audit the controls on a form to enter it in xml file (ControlAccessibilty.xml)  as shown below?
<Forms>
<Form1>
<Controls>
 <TextBox>
    <InUse>True</InUse>
    <Enabled>True</Enabled>
 </TextBox>
 <ComboBox>
    <InUse>True</InUse>
    <Enabled>True</Enabled>
 </ComboBox>
etc.

</Controls>
</Form1>
</Forms>

The following code could be used as starting point:
Dim controlsD As New Dictionary(Of String, String)
        For Each contrl As Control In Me.Controls
            controlsD.Add(contrl.Name, contrl.GetType.ToString)
        Next

        For Each key As String In controlsD.Keys
            Debug.WriteLine("Name = " & key & "; Type = " & controlsD(key))
        Next

Open in new window

0
Comment
Question by:Mike Eghtebas
2 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40395978
Potentially something like this:
Form1.vb -
Imports System.Xml
Imports System.Reflection

Public Class Form1

	Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		Using writer As XmlWriter = XmlWriter.Create("Controls.xml", New XmlWriterSettings() With {.Indent = True})
			With writer
				.WriteStartDocument()
				.WriteStartElement("Forms")
				.WriteStartElement(Me.GetType().ToString())
				.WriteElementString("Name", Name)
				For Each control As Control In Controls
					.WriteStartElement(control.GetType().ToString())
					.WriteElementString("Name", control.Name)
					For Each [property] As PropertyInfo In control.GetType().GetProperties()
						If [property].CanRead AndAlso [property].Name.Equals("InUse", StringComparison.InvariantCultureIgnoreCase) OrElse [property].Name.Equals("Enabled", StringComparison.InvariantCultureIgnoreCase) Then
							If Not [property].GetValue(control, Nothing) Is Nothing Then
								.WriteElementString([property].Name, [property].GetValue(control, Nothing).ToString())
							End If
						End If
					Next
					.WriteEndElement()
				Next
				.WriteEndElement()
				.WriteEndElement()
				.WriteEndDocument()
			End With
		End Using
	End Sub
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.CheckBox1 = New System.Windows.Forms.CheckBox()
		Me.TextBox1 = New System.Windows.Forms.TextBox()
		Me.ComboBox1 = New System.Windows.Forms.ComboBox()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'CheckBox1
		'
		Me.CheckBox1.AutoSize = True
		Me.CheckBox1.Location = New System.Drawing.Point(13, 13)
		Me.CheckBox1.Name = "CheckBox1"
		Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
		Me.CheckBox1.TabIndex = 0
		Me.CheckBox1.Text = "CheckBox1"
		Me.CheckBox1.UseVisualStyleBackColor = True
		'
		'TextBox1
		'
		Me.TextBox1.Location = New System.Drawing.Point(13, 36)
		Me.TextBox1.Name = "TextBox1"
		Me.TextBox1.Size = New System.Drawing.Size(100, 20)
		Me.TextBox1.TabIndex = 1
		'
		'ComboBox1
		'
		Me.ComboBox1.FormattingEnabled = True
		Me.ComboBox1.Items.AddRange(New Object() {"Apples", "Pears", "Oranges", "Bananas", "Cherries"})
		Me.ComboBox1.Location = New System.Drawing.Point(13, 62)
		Me.ComboBox1.Name = "ComboBox1"
		Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
		Me.ComboBox1.TabIndex = 2
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(197, 229)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(75, 23)
		Me.Button1.TabIndex = 3
		Me.Button1.Text = "Audit"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 264)
		Me.Controls.Add(Me.Button1)
		Me.Controls.Add(Me.ComboBox1)
		Me.Controls.Add(Me.TextBox1)
		Me.Controls.Add(Me.CheckBox1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)
		Me.PerformLayout()

	End Sub
	Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
	Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
	Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 40395982
Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now