欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 操作XML 按指定格式写入XML数据 WriteXml

程序员文章站 2024-03-31 20:21:46
复制代码 代码如下:private sub writexml() try create(strfname, "newdataset") savexmlfile("gg",...

复制代码 代码如下:

private sub writexml()
try
create(strfname, "newdataset")
savexmlfile("gg", "newdataset", true)
catch ex as exception
msgbox(ex.message)
end try
end sub
private function create(byval filename as string, byval root as string) as boolean
dim newxml as xmltextwriter = nothing
irowcount = objdataset.tables("dataanalyseview").rows.count
icolcount = objdataset.tables("dataanalyseview").columns.count
try
newxml = new xmltextwriter(filename, nothing)
newxml.formatting = formatting.indented
newxml.writestartdocument()
newxml.writecomment(my.application.info.assemblyname & " settings ") '项目名称
newxml.writestartelement(root)
newxml.writestartelement(objdataset.tables("dataanalyseview").tablename.tostring())
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(2).columnname().tostring())
newxml.writestring(objdataset.tables("serialnumbertable").rows(txtn)("serialnumber").tostring())
newxml.writeendelement()
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(3).columnname().tostring())
newxml.writestring(objdataset.tables("dataanalyseview").rows(0)("client").tostring())
newxml.writeendelement()
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(5).columnname().tostring())
newxml.writestring(objdataset.tables("dataanalyseview").rows(0)("productor").tostring())
newxml.writeendelement()
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(6).columnname().tostring())
newxml.writestring(objdataset.tables("dataanalyseview").rows(0)("model").tostring())
newxml.writeendelement()
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(4).columnname().tostring())
newxml.writestring(objdataset.tables("dataanalyseview").rows(0)("stationname").tostring())
newxml.writeendelement()
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(7).columnname().tostring())
newxml.writestring(objdataset.tables("dataanalyseview").rows(0)("datatime").tostring())
newxml.writeendelement()
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(8).columnname().tostring())
newxml.writestring(objdataset.tables("dataanalyseview").rows(0)("status").tostring())
newxml.writeendelement()
newxml.writeendelement()
progressbar1.step = 1
progressbar1.value = 1
progressbar1.visible = true
for irow = 0 to irowcount - 1
progress(irowcount - 1)
newxml.writestartelement(objdataset.tables("dataanalyseview").tablename.tostring())
for icol = 0 to icolcount - 1
if icol < 2 or icol > 7 then
newxml.writestartelement(objdataset.tables("dataanalyseview").columns(icol).columnname().tostring)
newxml.writestring(objdataset.tables("dataanalyseview").rows(irow)(icol).tostring())
newxml.writeendelement()
end if
next
newxml.writeendelement()
next
newxml.writeendelement()
newxml.writeendelement()
newxml.flush()
newxml.close()
catch ex as exception
debug.print("create - " & ex.message)
return false
finally
if newxml isnot nothing then
newxml.close()
newxml = nothing
end if
end try
return true
end function
private function savexmlfile(byval asection as string, byval akey as string, byval avalue as string) as boolean
dim paths() as string
dim n as integer
dim node, node2 as xmlnode
dim ele as xmlelement
while strings.left(asection, 1) = "/ "
asection = strings.mid(asection, 2)
end while
if asection = " " then
xmldoc.documentelement.removeall()
else
paths = strings.split(asection, "/ ")
try
node = xmldoc.documentelement.selectsinglenode(paths(n))
if node is nothing then
ele = xmldoc.createelement(paths(n))
node = xmldoc.documentelement.appendchild(ele)
end if
for n = 1 to paths.length - 1
if paths(n) = " " then continue for
node2 = node.selectsinglenode(paths(n))
if node2 is nothing then
ele = xmldoc.createelement(paths(n))
node2 = node.appendchild(ele)
end if
node = node2
next
if akey = " " then
node.removeall()
else
ele = node.item(akey)
if ele is nothing then
ele = xmldoc.createelement(akey)
node.appendchild(ele)
end if
if avalue = " " then
node.removechild(ele)
else
ele.innertext = avalue
end if
end if
catch ex as exception
debug.print(ex.message)
return false
end try
end if
xmldoc.save(strfilename)
end function