vb.net 读写xml方法(1)

 

 Dim domXmlDocument As System.Xml.XmlDocument
    Dim tmpPath As String = AppTempFilePath
    Dim xmlFile As String = tmpPath + "\testXml.xml" 

’窗体加载事件
    Private Sub TestXml_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ’读xml过程测试通过
        Dim domXmlDocument As System.Xml.XmlDocument
        Dim tmpPath As String = AppTempFilePath
        Dim xmlFile As String = tmpPath + "\testXml.xml"
        Dim reader As System.Xml.XmlReader = Nothing
        Try
            reader = New Xml.XmlTextReader(xmlFile)
            ’reader.
            While reader.Read
                Me.lboxXml.Items.Add(reader.Name + reader.Value)
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
    ’载入xml事件
    Private Sub btnXmlLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXmlLoad.Click
        ’Me.lboxXml.Items.Clear()
        ’’读xml过程测试通过
        ’Dim reader As System.Xml.XmlReader = Nothing
        ’Try
        ’    reader = New Xml.XmlTextReader(xmlFile)
        ’    While reader.Read
        ’        Me.lboxXml.Items.Add(reader.Name + ":" + reader.Value)
        ’    End While
        ’Catch ex As Exception
        ’    MsgBox(ex.Message)
        ’Finally
        ’    If Not (reader Is Nothing) Then
        ’        reader.Close()
        ’    End If
        ’End Try
        Dim ds As New DataSet
        Try
            ’如果直接使用ds做datasource则不会展开datagrid,用dv则可以直接显示正确。
            ds.ReadXml(xmlFile)
            Dim tb As DataTable
            Dim dv As DataView
            tb = ds.Tables(0)
            dv = New DataView(tb)
            DataGrid1.DataSource = dv
            ’DataGrid1.DataMember = "testXmlMember"
            ’DataGrid1.DataMember = "employeefname"
            ’Dim dxd As New XmlDataDocument
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    ’保存新建xml内容事件
    Private Sub btnSaveNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveNew.Click
        Dim myTW As New XmlTextWriter(tmpPath + "\testXmlWrite.xml", Nothing)
        myTW.WriteStartDocument()
        myTW.Formatting = Formatting.Indented
        myTW.WriteStartElement("Team")
        myTW.WriteStartElement("player")
        myTW.WriteAttributeString("Name", "George Zip")
        myTW.WriteAttributeString("Position", "QB")
        myTW.WriteElementString("Nickname", "Zippy")
        myTW.WriteElementString("JerseyNumber", XmlConvert.ToString(7))
        myTW.WriteEndElement()
        myTW.WriteEndElement()
        myTW.WriteEndDocument()
        myTW.Close()
    End Sub

对于修改datagrid中指定内容并保存到xml中还不会,弄明白了,在vb.net与xml读写的2中写出来!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值