vb.net 讀寫xml方法(1)
2024-07-10 13:00:55
供稿:網友
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中寫出來!