Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hi

i want to get my database connection string from a xml file, in my windows application. i'm using vb.net

 

i have no clue of how to create this xml file and to read that xml file in my code

 

can any body help me

thx

  • Administrators
Posted

If you just want to use this file for adminastrative control then using an application.config file may be the way to go.

If your application is name app1.exe then the config file would be called app1.exe.config

 

example of a file would be




	


 

In your code you could then do something like

Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader

dim connString as string = CType(configurationAppSettings.GetValue("ConnectionString", GetType(System.String)), String)

to read the value at runtime

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

hi PlausiblyDamp

thank you very much for the relpy

 

yes i can use the application.config file to do this.

 

but if i want to keep this information in a normal xml file, how can i do that?

 

thank you

Posted

you can use MSXML 4 class from http://www.microsoft.com/downloads/details.aspx?FamilyID=3144b72b-b4f2-46da-b4b6-c5d7485f2b42&displaylang=en

 

declare object as,

Dim objDoc2 As New MSXML2.DOMDocument30()

 

 

i had used following code:

 

 

If objDoc2.load(Directory.GetCurrentDirectory & "\config.xml") Then

If Not objDoc2 Is Nothing Then

pathtosavexmlfiles = objDoc2.getElementsByTagName("storepath_mails_invoices").item(0).text

path_of_the_database = objDoc2.getElementsByTagName("dataSource").item(0).text

If Not (pathtosavexmlfiles.Substring(pathtosavexmlfiles.Length - 1) = "\") Then

pathtosavexmlfiles = pathtosavexmlfiles & "\"

'If a back slash is missing from the path, it will be inserted here

End If

'Dim path_of_the_database As String

End If

End If

 

 

here the xml file is config.xml and "storpath_mails_invoices" is the tag name which reads the path to which files are to be saved(this is happening in my case........u can give the path to do anything else).The config.xml file exists in the current directory.

 

i think this will be sufficient.if you have any other doubts feel free to ask

  • Administrators
Posted

Rather than using a COM dll for XML manipulation you could use the built in framework support.

 

A simple example of how to store user settings could be done like this:

 

Create a class to hold your settings

Public Class Settings
   Public connectopnString As String = "FSDFSDFSDFF"
   'add other properties if need be
End Class

 

to write to it use

       Dim s As New Settings
       Dim x As New Xml.Serialization.XmlSerializer(s.GetType)

       Dim fs As System.IO.FileStream = System.IO.File.OpenWrite("c:\test.xml")
       x.Serialize(fs, s)
       fs.Close()

 

and to read a value back

       Dim s As New Settings
       Dim x As New Xml.Serialization.XmlSerializer(s.GetType)

       Dim fs As System.IO.FileStream = System.IO.File.OpenRead("c:\test.xml")
       s = DirectCast(x.Deserialize(fs), Settings)
       fs.Close()
       MessageBox.Show(s.connectopnString)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...