XML 读取/更改/保存 vb.net
Posted
技术标签:
【中文标题】XML 读取/更改/保存 vb.net【英文标题】:XML read/change/save vb.net 【发布时间】:2016-04-20 17:38:53 【问题描述】:有没有一种简单的方法可以在下面的xml中更改IP地址?
我的想法是先在列表框中加载“连接名称”,然后再加载 IP 地址。 然后制作第三个列表框,他们可以在其中添加新的 IP 地址。
然后保存新的 XML 文件
<CSD>
<Version>
<Version>2.0.1</Version>
<fileVersion>2.0</fileVersion>
</Version>
<Connections>
<Connection name="Con1">
<Property name="attributes">
<title>Product1</title>
<ipaddress>10.79.44.33</ipaddress>
<ipport>80</ipport>
</Property>
</Connection>
<Connection name="Con2">
<Property name="attributes">
<title>Product2</title>
<ipaddress>10.79.44.34</ipaddress>
<ipport>80</ipport>
</Property>
</Connection>
<Connection name="Con3">
<Property name="attributes">
<title>Productq</title>
<ipaddress>10.79.44.35</ipaddress>
<ipport>80</ipport>
</Property>
</Connection>
</Connections>
</CSD>
【问题讨论】:
【参考方案1】:试试这个
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim xml As String = _
"<CSD>" & _
"<Version>" & _
"<Version>2.0.1</Version>" & _
"<fileVersion>2.0</fileVersion>" & _
"</Version>" & _
"<Connections>" & _
"<Connection name=""Con1"">" & _
"<Property name=""attributes"">" & _
"<title>Product1</title>" & _
"<ipaddress>10.79.44.33</ipaddress>" & _
"<ipport>80</ipport>" & _
"</Property>" & _
"</Connection>" & _
"<Connection name=""Con2"">" & _
"<Property name=""attributes"">" & _
"<title>Product2</title>" & _
"<ipaddress>10.79.44.34</ipaddress>" & _
"<ipport>80</ipport>" & _
"</Property>" & _
"</Connection>" & _
"<Connection name=""Con3"">" & _
"<Property name=""attributes"">" & _
"<title>Productq</title>" & _
"<ipaddress>10.79.44.35</ipaddress>" & _
"<ipport>80</ipport>" & _
"</Property>" & _
"</Connection>" & _
"</Connections>" & _
"</CSD>"
Dim csd As XElement = XElement.Parse(xml)
Dim ipAddresses As List(Of XElement) = csd.Descendants("ipaddress").ToList()
Dim subAddress As String = "10.79.40"
Dim address = 1
For Each ipAddress As XElement In ipAddresses
ipAddress.Value = subAddress & "." & address.ToString
address += 1
Next ipAddress
End Sub
End Module
【讨论】:
以上是关于XML 读取/更改/保存 vb.net的主要内容,如果未能解决你的问题,请参考以下文章
vb.net 2.0 如何用Configuration.ConfigurationSettings.AppSettings读取配置文件中的内容?