0

Here is both the XML file and my code. I need to change the number 2509 in the <Value> tag. The code below finds the <Value>SchoolFiles</Value> but then jumps out of the loop before. I know there is a much better way then looping.

XML File

<Filters>
    <Filter>
        <Name>ViewFolders</Name>
        <ApplyToFiles>0</ApplyToFiles>
        <ApplyToDirs>1</ApplyToDirs>
        <MatchType>None</MatchType>
        <MatchCase>0</MatchCase>
        <Conditions>
            <Condition>
                <Type>0</Type>
                <Condition>0</Condition>
                <Value>SchoolFiles</Value>
            </Condition>
            <Condition>
                <Type>0</Type>
                <Condition>0</Condition>
                <Value>DataImportFiles</Value>
            </Condition>
            <Condition>
                <Type>0</Type>
                <Condition>0</Condition>
                <Value>2509</Value>
            </Condition>
        </Conditions>
    </Filter>
</Filters>
Dim FileZillaXMLFilterFile As String = "C:\Users\Development\AppData\Roaming\FileZilla\filters.xml"
Dim myXmlDocument As XmlDocument = New XmlDocument()
myXmlDocument.Load(FileZillaXMLFilterFile)
Dim MyNode As XmlNode
Dim node As XmlNode
node = myXmlDocument.SelectSingleNode("//Filters")
'node = myXmlDocument.SelectSingleNode("//Filters/Filter/Conditions/Condition/Value")
For Each book As XmlNode In node.ChildNodes

    Debug.Print(book.InnerText) '= Session("iCustID").ToString()
    If Microsoft.VisualBasic.Left(book.InnerText, 11) = "ViewFolders" Then
        For Each node4 As XmlNode In book.ChildNodes
            If node4.Name = "Conditions" Then
                'Debug.Print(node4.LastChild.Name)

                For Each node5 As XmlNode In node4.ChildNodes
                    Debug.Print(node5.Name)
                    For Each node6 As XmlNode In node5.ChildNodes
                        Debug.Print(node6.Name)
                        If node6.Name = "Value" Then
                            Debug.Print(node6.InnerText)
                            For Each node7 As XmlNode In node6.ChildNodes
                                If node7.InnerText <> "SchoolFiles" And node7.InnerText <> "DataImportFiles" Then
                                    'need to change 2509 to another numnber
                                    Debug.Print(node7.InnerText)
                                End If
                            Next
                        End If
                    Next
                Next
            End If
        Next
    End If
Next
2

1 Answer 1

1

The trick to doing this is to use XPath to find the right node and then change it's value. You can then use the XmlDocument to rewrite the file. The XPath of the needed node is:

/Filters/Filter/Conditions/Condition[Value != 'SchoolFiles' and Value != 'DataImportFiles']/Value

Here I replicated your logic of checking for SchoolFiles and DataImportFiles, but there might be better ways to focus in on the correct node. For example, it might be based on it's index. Or if the Type nodes had unique values, you could use that.

I see in your code that you already are using SelectSingleNode. You can use that with this XPath to save a lot of looping. You can the set the node's Value property to a new number, and save the XMLDocument using it's Save method.

A tool like XMLSpy is useful in getting the XPath correct. The W3Schools (http://www.w3schools.com/xml/) is a good place to learn XML and XPath.

Sign up to request clarification or add additional context in comments.

2 Comments

One of the issues with this .XML file is it has repeating tags, I've just shown part of it. So your example finds the top section. which is why I went down the looping path. I don't really want to paste the whole file in here and I don't see where I can upload any files?
OK I got it too work using my original code and your suggestion of Saving... If node7.InnerText <> "SchoolFiles" And node7.InnerText <> "DataImportFiles" Then 'need to change 2509 to another numnber Debug.Print(node7.InnerText) node7.Value = "2509" myXmlDocument.Save(FileZillaXMLFilterFile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.