Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aceessing an XML config file from VB2008 1

Status
Not open for further replies.

DPlank

IS-IT--Management
May 30, 2003
1,903
GB
I'm having trouble getting my application to use the config file I've set up for it.

I can get the config file and it's being found correctly (I've checked the path) - but it fails on the oXmlDoc.Load command.

I've got references in for System.XML and MSXML2 just in case. Can anyone see where I might be going wrong?

Code:
        oShell = CreateObject("Shell.Application")
        oFolder = oShell.Namespace(DESKTOP)
        sDesktop = oFolder.self.path
        sConfigFile = """" & sDesktop & "\SCMS_Config.xml"""
        oFolder = Nothing
        oShell = Nothing

        ' Parse the config file
        Dim oXmlDoc
        oXmlDoc = CreateObject("Microsoft.XMLDOM")
        oXmlDoc.async = "false"
        If oXmlDoc.Load(sConfigFile) = False Then
            ' The document failed to load.

I've inherited this code (which was originally written in VBscript) so I'm not entirely sure about the .async = "false" line either...

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
If you are loading an XML document (config file) then I would probably recommend using XMLDocument from within the .net framework. You can then use the XMLDocument.Load(filename) rather than creating an XMLDOM object.

Just a first recommendation.

You also might consider using the File object to check for the existence of the file (IsExists I think). That way if the file doesn't exist you can throw an error to the user.
 
I've been fiddling with it and have amended it to read:
Code:
 Dim oXmlDoc As New XmlDocument
 oXmlDoc.Load(sConfigFile)

However I'm getting an exception due to "Illegal characters in path" which is the sConfigFile variable, set as shown in previous code
Code:
oFolder = oShell.Namespace(DESKTOP)
        sDesktop = oFolder.self.path
        sConfigFile = """" & sDesktop & "\SCMS_Config.xml"""
This resolves sConfigFile to ""C:\Documents and Settings\DPL05\Desktop\SCMS_Config.xml""

I can't see where there are any illegal characters in there..?

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
Also just a thought (being a c# programmer) there are escape characters like \n, \t etc. Might be worth a check that these \ escape characters are not the same in VB.Net.

One way to get round that is you use
@"C:\Documents and Settings\DPL05\Desktop\SCMS_Config.xml"

or you could use

C:\\Documents and Settings\\DPL05\\Desktop\\SCMS_Config.xml

Like I say this might just be for C# as it has been a while since I touched VB
 
The initial quotes are to show that sConfigFile holds a string, the second set (as I understand it) are present to cover the space characters in the 'Documents and Settings' part...

I'll give your suggestions a try to see what happens - I've seen \ used to signify a literal or a regexp before, so you're probably right.

And you were!

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
To clarify: I needed the "\\" installed through the path, so a quick Replace allowed me to resolve the problem. This, followed by a Left and Right to delete the extra " marks which were added by the self.path section provided me with the acceptble filename. Corrected code below:

Code:
Dim sConfigFile As String
        oShell = CreateObject("Shell.Application")
        oFolder = oShell.Namespace(DESKTOP)
        sDesktop = oFolder.self.path
        sConfigFile = """" & sDesktop & "\SCMS_Config.xml"""
        sConfigFile = Replace(sConfigFile, "\", "\\")
        sConfigFile = Left(sConfigFile, Len(sConfigFile) - 1)   ' Cuts off the trailing " character
        sConfigFile = Right(sConfigFile, Len(sConfigFile) - 1)    ' Cuts off the leading " character

Thanks again!

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top