当前位置:编程学习 > C#/ASP.NET >>

vb.net 中读取xml节点的值,并修改此节点的值!!!

<Reports>
  <!--Report *** HR001L01 ***-->
  <Report version="2.5.20063.223">
    <Name>HR001L01</Name>
    <ReportInfo>
      <Author>hx0218</Author>
    </ReportInfo>
    <DataSource>
      <ConnectionString>Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TPT-UP;Data Source=HX-1025</ConnectionString>
    </DataSource>
    <Layout>
      <Width>15660</Width>
      <MarginLeft>720</MarginLeft>
      <MarginTop>720</MarginTop>
      <MarginRight>720</MarginRight>
      <MarginBottom>720</MarginBottom>
      <Orientation>2</Orientation>
      <PaperSize>9</PaperSize>
    </Layout>
 </Report>
..............

如上所示为XML文件中的内容,我现在想实现的是读取这个xml文件,并遍历所有的xml中的节点,如果节点为ConnectionString 则修改这个节点的值,不知道怎么样才能实现,而且在xml文件中可能会有多个ConnectionString 这个节点,只不过可能父节点可能不一样??请高手帮帮忙!!!!
--------------------编程问答-------------------- Dim xmldoc As New Xml.XmlDocument
xmldoc.Load(Application.StartupPath & "\XMLSet.xml")
读取xmldoc.Item(Type1).Item(type2).InnerText()

 赋值xmldoc.Item(Type1).Item(Type2).InnerText = Value --------------------编程问答-------------------- 给个我的你看下,希望能帮到你;
    ''读取xml
    Sub Read_XML()
        Try
            If My.Computer.FileSystem.FileExists(xmlFileName) Then
                Dim doc As New Xml.XmlDocument
                doc.Load(xmlFileName)
                Dim re As Xml.XmlNodeReader = New Xml.XmlNodeReader(doc)

                While re.Read
                    Select Case re.NodeType
                        Case Xml.XmlNodeType.Element
                            Name = re.Name
                        Case Xml.XmlNodeType.Text
                            If Name.Equals("pinghaolm") Then
                                Txt_pinghaolm.Text = re.Value.Trim
                            End If
                            If Name.Equals("pingminglm") Then
                                Txt_pingminglm.Text = re.Value.Trim
                            End If
                            If Name.Equals("guigelm") Then
                                Txt_guigelm.Text = re.Value.Trim
                            End If
                            If Name.Equals("danweilm") Then
                                Txt_danweilm.Text = re.Value.Trim
                            End If
                            If Name.Equals("pihaolm") Then
                                Txt_pihaolm.Text = re.Value.Trim
                            End If
                            If Name.Equals("cangkumclm") Then
                                Txt_cangkumclm.Text = re.Value.Trim
                            End If
                            If Name.Equals("benqirklm") Then
                                Txt_benqirklm.Text = re.Value.Trim
                            End If
                            If Name.Equals("benqicklm") Then
                                Txt_benqicklm.Text = re.Value.Trim
                            End If
                            If Name.Equals("qimokclm") Then
                                Txt_qimokclm.Text = re.Value.Trim
                            End If
                            If Name.Equals("zuihoujcsjlm") Then
                                Txt_zuihoujcsjlm.Text = re.Value.Trim
                            End If
                    End Select
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & ex.StackTrace)
        End Try
    End Sub

    ''''修改xml
    Sub Update_xml()
        Dim elementNameArry As String() = {"pinghaolm", "pingminglm", "guigelm", "danweilm", "pihaolm", "cangkumclm", "benqirklm", "benqicklm", "qimokclm", "zuihoujcsjlm"}
        Dim rootName As String = "SZLM"
        If My.Computer.FileSystem.FileExists(xmlFileName) Then
            Dim doc As New Xml.XmlDocument
            doc.Load(xmlFileName)
            Dim list As Xml.XmlNodeList = doc.SelectSingleNode(rootName).ChildNodes
            For Each xn As Xml.XmlNode In list
                Dim xe As Xml.XmlElement
                xe = xn
                For i As Integer = 0 To elementNameArry.Length - 1
                    If xe.Name = "pinghaolm" Then
                        xe.InnerText = Txt_pinghaolm.Text.Trim
                    End If
                    If xe.Name = "pingminglm" Then
                        xe.InnerText = Txt_pingminglm.Text.Trim
                    End If
                    If xe.Name = "guigelm" Then
                        xe.InnerText = Txt_guigelm.Text.Trim
                    End If
                    If xe.Name = "danweilm" Then
                        xe.InnerText = Txt_danweilm.Text.Trim
                    End If
                    If xe.Name = "pihaolm" Then
                        xe.InnerText = Txt_pihaolm.Text.Trim
                    End If
                    If xe.Name = "cangkumclm" Then
                        xe.InnerText = Txt_cangkumclm.Text.Trim
                    End If
                    If xe.Name = "benqirklm" Then
                        xe.InnerText = Txt_benqirklm.Text.Trim
                    End If
                    If xe.Name = "benqicklm" Then
                        xe.InnerText = Txt_benqicklm.Text.Trim
                    End If
                    If xe.Name = "qimokclm" Then
                        xe.InnerText = Txt_qimokclm.Text.Trim
                    End If
                    If xe.Name = "zuihoujcsjlm" Then
                        xe.InnerText = Txt_zuihoujcsjlm.Text.Trim
                    End If
                Next
            Next
            doc.Save(xmlFileName)
        End If
    End Sub

--------------------编程问答-------------------- Dim node As XmlNode = doc.SelectSingleNode("//Reports/Report/DataSource/ConnectionString")
If Not node Is Nothing Then
     node.InnerText = "test"
End If
补充:.NET技术 ,  VB.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,