Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

vb.net - how to Update a node in xml?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...