vb.net read-Write XML method (1)

Source: Internet
Author: User
XML Dim domxmldocument as System.Xml.XmlDocument
Dim Tmppath as String = Apptempfilepath
Dim xmlFile as String = Tmppath + "\testxml.xml"

Form Load Events
Private Sub testxml_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Read XML process Test pass
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 are nothing) Then
Reader. Close ()
End If
End Try
End Sub
Loading XML events
Private Sub Btnxmlload_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnxmlload.click
Me.lboxXml.Items.Clear ()
Read XML process Test pass
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 are nothing) Then
Reader. Close ()
End If
End Try
Dim DS as New DataSet
Try
If you use DS directly to do datasource, you will not expand the DataGrid, with DV can be directly displayed correctly.
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
Save new XML Content Event
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

Not to modify the specified content in the DataGrid and save it in XML, it is clear, written in the 2 of vb.net and XML read and write!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.