-2

I want to update a node in vb.net using xml but I cannot find a proper solution.

This is what I have for reading and writing:

  If (IO.File.Exists(Application.StartupPath & "MyXML.xml")) Then
        Dim document As XmlReader = New XmlTextReader("MyXML.xml")
        While (document.Read())
            Dim type = document.NodeType
            If (type = XmlNodeType.Element) Then
                If (document.Name = "port") Then
                    port = document.ReadInnerXml.ToString()
                End If
            End If
        End While
    Else
        Dim settings As New XmlWriterSettings()
        settings.Indent = True
        Dim XmlWrt As XmlWriter = XmlWriter.Create("MyXML.xml", settings)
        With XmlWrt
            .WriteStartDocument()
            .WriteComment("XML.")
            .WriteStartElement("Data")
            .WriteStartElement("Settings")
            .WriteStartElement("port")
            .WriteString("7008")
            .WriteEndElement()
            .WriteEndDocument()
            .Close()
        End With

    End If
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Danny
  • 15
  • 1
  • 1
  • 4
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 17 '14 at 17:09
  • 1
    Please be more specific. What is the code supposed to do? What is it doing that it should be? What (if any) error messages does it generate? – amphetamachine Dec 17 '14 at 17:29

2 Answers2

4

Here's a little start for updating an XML node, please see below for example. You'll need to make a few adjustments as necessary to fit your needs. The code you provided is only for reading and creating an XML file, not updating one as you indicated. With this in mind I'm not sure what node you're looking for (maybe "node"), but in my example...

"YOURNODE" - Change this to the node you are looking for

"NEW NODES TEXT" - Change this to what you want the node to be (text)

Dim MyXML As New XmlDocument()
MyXML.Load(Application.StartupPath & "MyXML.xml")
Dim MyXMLNode As XmlNode = MyXML.SelectSingleNode("YOURNODE")

'If we have the node let's change the text
If MyXMLNode IsNot Nothing Then
  MyXMLNode.ChildNodes(0).InnerText = "NEW NODES TEXT"
Else
  'Do whatever 
End If 

'Save the XML now
MyXML.Save(Application.StartupPath & "MyXML.xml")

Code Edit Per Your Requirements

We have to dig down into the child nodes, once we have it we can change that text...

  Dim MyXML As New XmlDocument()
    MyXML.Load(Application.StartupPath & "MyXML.xml")
    Dim MyXMLNode As XmlNode = MyXML.SelectSingleNode("Data")

    'If we have the node let's change the text
    If MyXMLNode IsNot Nothing Then
        Dim MyXMLNodes As XmlNodeList
        MyXMLNodes = MyXMLNode.ChildNodes(0).ChildNodes

        'Set the nodes text
        If MyXMLNodes IsNot Nothing Then
            MyXMLNodes.Item(0).InnerText = "NEW NODES TEXT"
        End If

        'Save the XML now
        MyXML.Save(Application.StartupPath & "MyXML.xml")

    End If
Community
  • 1
  • 1
Trevor
  • 7,777
  • 6
  • 31
  • 50
0

Solution using Powershell

assume you need to change the value inside tag2, which is inside tag1:

1)save below code with .ps1 extenstion, say example.ps1

$xml = [xml](Get-Content .\inputfile.xml)
$xml.tag1.tag2="newvalue"
$xml.Save(".\inputfile.xml")

2)now save below code as .bat file in same folder, say editxml.bat

Powershell.exe -File  example.ps1

3)execute this bat from VB.NET using

process.start("editxml.bat")
anandhu
  • 686
  • 2
  • 13
  • 40