Wednesday, June 30, 2010

What School Did Myamee Go Tofam U

Load data from an XML file in VB.Net

If you're one of those guys who do not want life difficult trying to store in a database each parameter that was presented derrepente to initialize the application that opens the doors of your system, as I have here a practical solution in VB.net using file XML boot, maybe the term you paresca strange but it's not complicated if you only want to initialize only values \u200b\u200bin a form whatsoever, and here is the solution: Just just place the following code into a file text and change its original extension (. txt) and XML for this to become as such, or perhaps have been more inquisitive and I have noticed that there is a section in the versions of VB.Net to create this file: Menu Project -> Add New Data Element ...--> ---> XML File




XML:


< companies >

< company name = " EMPRESA1 " bd =" BD_ EMPRESA1 "/>

< company name = " EMPRESA2 " bd = " BD_ EMPRESA2 "/>

< company name = " EMPRESA3 " bd = " BD_ EMPRESA3 " />

< company name = " EMPRESA4 " bd = " BD_ EMPRESA4 "/>

companies >


VB.net Code :


Dim _dsdetalle As DataSet 'I have decided to fill the xml data in a data set in the first instance as this me serve to link or fill in the data after a certain combo that eleji to display the information contained in the xml

Private Sub FRM_Load ( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load

Dim Xml As XmlDocument

Dim NodeList As XmlNodeList

Dim Node As XmlNode

Dim _DS As New DataSet

LlenarDS()

Try

Xml = New XmlDocument ()

XML.load (Application.StartupPath & "\\ Inicio.xml " ) 'eye this is the address of the folder where you run the program (software, application, system, etc) because that is where prinicipal I have kept Inicio.xml



NodeList = Xml.SelectNodes( "/empresas/empresa" )

For Each Node In NodeList

Dim rw As DataRow = _dsdetalle.Tables (0). NewRow

rw ( "razon_social" ) = Node.Attributes . getNamedItem ( " name " ) . Value

rw ( "bd" ) = Node.Attributes . GetNamedItem ( "bd" ). Value

_dsdetalle.Tables (0). Rows.Add (rw)

Next

CboEmpresa.DisplayMember = "razon_social"

CboEmpresa.ValueMember = "bd"

CboEmpresa.DataSource _dsdetalle.Tables = (0)

Catch ex As Exception

MsgBox(ex.GetType.ToString &amp; vbNewLine &amp; ex.Message.ToString)

Finally

Console.Read()

End Try

End Sub

'We shape our dataset in a separate thread that links your content to the combo

Private Sub LlenarDS ()

_dsdetalle = New DataSet

_dsdetalle.Tables.Add(0)

With _dsdetalle.Tables(0).Columns

.Add( "razon_social" , System.Type.GetType( "System.String" ))

.Add( "Bd" , System.Type.GetType ( "System.String" ))

End With

End Sub

0 comments:

Post a Comment