如何用VB.NET编写XML文档
使用xmltextwriter类要遵循以下五个步骤:
1、创建新的xml流,并使用writestartdocument( )方法。这个方法会在文件中写入适当的xml声明。
2、用writestartelement( )方法写根数据元素。
3、在文件中写其余的元素。
4、用writeendelement( )方法关闭最后的元素。
5、用writeenddocument( ) 方法结束xml数据。调用这一方法很重要。该方法会关闭没有适当关闭的元素与其他结构。
以下代码:
在窗体文件的最开头:
imports system.xml
按钮的click事件:
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim mytw as new xmltextwriter(application.startuppath & "\mytest.xml", nothing)
mytw.writestartdocument()
... mytw.formatting = formatting.indented
mytw.writestartelement("player")
mytw.writestartelement("team")
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
执行程序,单击按钮后,在bin文件夹下就会出现已经写好的mytest.xml文档!