Change node iteratively

kasteel

Well-known member
Joined
May 29, 2009
Messages
50
Programming Experience
10+
Hi

I am useless when it comes to xml.

I have multiple computer names in groups. (About 20 groups and each group contains 12 computer names all in one xml file.
(defined as <name>Computer1</name>)
In total there are 300 computer names in an existing xml file. How can I rename each computer name iteratively in the xml file?

Example:
Computer1
Computer2
Computer3 (All the way to 300)

Only the <name>Computer1</name> node should be renamed. Any help would be appreciated. :)

Here is an example of the xml file:

VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<RDCMan schemaVersion="1">
    <version>2.2</version>
    <dfile>
        <properties>
            <name>TEST</name>
            <expanded>True</expanded>
            <comment />
            <logonCredentials inherit="FromParent" />
            <connectionSettings inherit="FromParent" />
            <gatewaySettings inherit="FromParent" />
            <remoteDesktop inherit="FromParent" />
            <localResources inherit="FromParent" />
            <securitySettings inherit="FromParent" />
            <displaySettings inherit="FromParent" />
        </properties>
        <group>
            <properties>
                <name>Group1</name>
                <expanded>True</expanded>
                <comment />
                <logonCredentials inherit="FromParent" />
                <connectionSettings inherit="FromParent" />
                <gatewaySettings inherit="FromParent" />
                <remoteDesktop inherit="FromParent" />
                <localResources inherit="FromParent" />
                <securitySettings inherit="FromParent" />
                <displaySettings inherit="FromParent" />
            </properties>
            <server>
                <name>Computer1</name>
                <displayName>Computer1</displayName>
                <comment />
                <logonCredentials inherit="None">
                    <userName>User1</userName>
                    <domain>DOMAIN</domain>
                    <password storeAsClearText="True">Password</password>
                </logonCredentials>
                <connectionSettings inherit="FromParent" />
                <gatewaySettings inherit="FromParent" />
                <remoteDesktop inherit="FromParent" />
                <localResources inherit="FromParent" />
                <securitySettings inherit="FromParent" />
                <displaySettings inherit="FromParent" />
            </server>
            <server>
                <name>Computer2</name>
                <displayName>Computer2</displayName>
                <comment />
                <logonCredentials inherit="None">
                    <userName>User2</userName>
                    <domain>DOMAIN</domain>
                    <password storeAsClearText="True">Password</password>
                </logonCredentials>
                <connectionSettings inherit="None">
                    <connectToConsole>True</connectToConsole>
                    <startProgram />
                    <workingDir />
                    <port>3389</port>
                </connectionSettings>
                <gatewaySettings inherit="FromParent" />
                <remoteDesktop inherit="FromParent" />
                <localResources inherit="FromParent" />
                <securitySettings inherit="FromParent" />
                <displaySettings inherit="FromParent" />
            </server>
 
For .Net 2 I would load a XmlDocument, select and iterate the //server/name nodes to set their InnerText. For example:
Dim doc As New Xml.XmlDocument
doc.Load("file.xml")
Dim servers As Xml.XmlNodeList = doc.SelectNodes("//server/name")
For ix As Integer = 0 To servers.Count - 1
    servers(ix).InnerText = "Computer" & (ix + 1).ToString
Next
doc.Save("file.xml")
 

Latest posts

Back
Top