vb.net中怎么创建xml文件并写数据

vb.net中怎么创建xml文件并写数据,第1张

DataSet 和 DataTable都有现成的方法:WriteXml

DataTable tb = this.dataGridView1.DataSource as DataTable

if(tb != null)

{

tb.WriteXml(@"C:\table.xml",true)

return

}

DataView dv = this.dataGridView1.DataSource as DataView

if(dv != null)

{

dv.Table.WriteXml(@"C:\table.xml",true)

return

}

IList list = this.dataGridView1.DataSource as IList

if(list != null)

{

//to do,如果是IList,就要你自己想办法导出了

//XmlDocument or XmlWriter都可以考虑

}

DataSet

DataTable

都有现成的方法:WriteXml

DataTable

tb

=

this.

dataGridView

1.DataSource

as

DataTable

if(tb

!=

null)

{

tb.WriteXml(@"C:\table.xml",true)

return

}

DataView

dv

=

this.dataGridView1.DataSource

as

DataView

if(dv

!=

null)

{

dv.Table.WriteXml(@"C:\table.xml",true)

return

}

IList

list

=

this.dataGridView1.DataSource

as

IList

if(list

!=

null)

{

//to

do,如果是IList,就要你自己想办法导出了

//XmlDocument

or

XmlWriter

都可以考虑

}

直接把xml内容写入string变量中,再把string变量保存成扩展名为xml的文件行吗

像这样:

dim strXml as string

strXml = "<TAXPAYER NSRMC=""中原特钢股份有限公司电力分公司"" NSRSBH=""410881789193697"" RECORDCOUNT=""1"" />"

然后将strXml写入文件

给你两个函数:

'***************************************************************

'将字符串strTmp写入路径为filePath 文件的函数

Private Sub WriteFile(filePath As String, strTmp As String)

Open filePath For Output As #1

Print #1, strTmp

Close #1

End Sub

'***************************************************************

'读文件返回字符串函数

Private Function ReadFile(filePath As String) As String

Dim fileStr As String

Open filePath For Input As #1

Do While Not EOF(1)

Line Input #1, tem

fileStr = fileStr &tem &vbCrLf

Loop

Close #1

ReadFile = fileStr

End Function

'***************************************************************

写入方法:

WriteFile app.path &"\xmlfile.xml",strXml

---------------------------------------------------------------

补充:

生成这句:

str="<TAXPAYER NSRMC=""中原特钢股份有限公司电力分公司"" NSRSBH=""410881789193697"" RECORDCOUNT=""1"" />

"

str存储的就是你要生成的那句,在字符串中要想引用双引号,就用两个双引号表示


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11969108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存