简体   繁体   中英

How to read / rewrite text file in vb.net?

I have an XML file like this

<serverlist>
      <server>
        <id>1</id>
        <ip>127.0.0.1</ip>
        <port>11101</port>
        <category sort="1">PVE</category>
        <name raw_name="TEST">
            <![CDATA[TEST]]>
        </name>

I want to read / rewrite the 127.0.0.1 Only and put it in a text box or from the text box to the file.

It seems an XML, so you can use LinQ to XML.

To get the value:

Private Function getValue(name As String) As String
    Dim doc = XDocument.Load("YourFilePath")
    Dim data = (From c In doc.Descendants(name)
                        Select c).First()
    Return data.Value
End Function

To set the value:

Private Sub setValue(name As String, value As String)
    Dim doc = XDocument.Load("YourFilePath")
    Dim data = (From c In doc.Descendants(name)
                        Select c).First()
    data.ReplaceAll(value)
    doc.Save("YourFilePath")
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM