注册 登录
编程论坛 VB.NET论坛

VB.net XML文件解析修改_求助

a6681316 发布于 2019-02-20 09:45, 2167 次点击
有这样一个xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appsettings>
  <Bassettings>
   <settingname="A" ser="String">
    <value>0<value>
      </setting>
   <settingname="B" ser="String">
    <value>10000<value>
      </setting>

   </Bassettings>
</appsettings>
</configuration>

如何获取A的value,和B的value,并且修改保存 麻烦大神解决下
1 回复
#2
厨师王德榜2019-02-22 22:51
程序代码:
    Private Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click
        '测试用 获取A的value,和B的value,并且修改保存
        Dim cpath As String = "E:\vbnet.prj\ModifyFileZillaXml\ModifyFileZillaXml\bin\Debug\sets.xml"
        Dim xd As New Xml.XmlDocument
        Dim aValue As String = "", bValue As String = "", cSettingName As String
        xd.Load(cpath)
        For Each nodeSets As Xml.XmlNode In xd.SelectSingleNode("configuration").SelectSingleNode("appsettings").SelectSingleNode("Bassettings")
            cSettingName = nodeSets.Attributes("name").Value
            Select Case cSettingName
                Case "A"
                    aValue = nodeSets.InnerText
                    nodeSets.ChildNodes(0).ChildNodes(0).Value = "99"
                Case "B"
                    bValue = nodeSets.InnerText
                    nodeSets.ChildNodes(0).ChildNodes(0).Value = "999"
            End Select
        Next
        '写到硬盘,释放内存。
        Dim tr As Xml.XmlTextWriter = New Xml.XmlTextWriter(cpath, Nothing) '写XML文件。
        tr.Formatting = Xml.Formatting.Indented
        xd.WriteContentTo(tr)
        tr.Close() '释放内存。
        Debug.Print("setting A.OldValue=" & aValue & ",修改后的值:99")
        Debug.Print("setting B.OldValue=" & bValue & ",修改后的值:999")
    End Sub

运行结果:
setting A.OldValue=0,修改后的值:99
setting B.OldValue=10000,修改后的值:999
1