Reading XML file and modify in VS.Net

ripon

Member
Joined
Mar 23, 2006
Messages
15
Programming Experience
Beginner
Hi All: I have a directory which has some "XML Configuration File" (as shown under type in folder in explorer). But in the directory none of these files show the extension .config. The directory files are like C0150,C0150_new,C0200,C0200_new and more. I need to change only 1 attribute of each of these files. I am asked to write a VS.Net project to do this. I have to send the .exe file(from bin directory of this VS.Net project) to my client and they will just double click the .exe file and it will change all the .config files.
The C0150 file looks like this:
<?xml version="1.0" encoding="utf-8"?>

<configuration>

<TechSupport>
<add key="UserName" value="CPIPDA" />
<add key="Password" value="Caseman2k6" />
</TechSupport>
<CASEMAN_Database>
<add key="UserID" value="cm_dba" />
<add key="Password" value="cm_dba.spam" />
</CASEMAN_Database>

<Afaria>
<add key="Channel" value="M0001" />
<add key="Address" value="142.206.214.14" />
</Afaria>
</configuration>

=========All I have been asked to change the value of key="Address" under "Afaria" node.

=========I tried the following code. I opened a VB.net project(MyProj.exe) in VS2003.Net(only available tool so far).

----------------
Imports System.windows.forms
Imports System.IO
Imports System.Xml
Imports System.Configuration
Imports System.Reflection
Module Module1
Sub Main()
'Get the current path from where we will run the MyProj.exe
'Its the same path where the .config files will be
Dim astr = System.Windows.Forms.Application.StartupPath
'Get all files
Dim entries() As String = Directory.GetFiles(astr)
Dim entry, stemp As String
Dim node As XmlNode
Dim elem As XmlElement
Dim doc As XmlDocument = New XmlDocument
For Each entry In entries
If Left(entry, 1) = "C" Then

doc.Load(entry) --Its crushing here and below
node = doc.SelectSingleNode("//Afaria")
elem = node.SelectSingleNode(String.Format("//add[@key='{0}']", "Address"))
If (IsNothing(elem)) Then
stemp = elem.GetAttribute("value")
End If
End If
Next
End Sub
End Module

============
Pls help me and thanks in advance.
Ripon
 
Back
Top