本文介绍了保存并使用vb.net将数据检索到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友您好,我尝试使用vb.net在xml文件中保存和检索数据.首先,我从添加新项中添加了一个xml文件(emp.xml)->向其中添加一些虚拟数据,例如
Hello friends I trying to save and retrieve data in xml file using vb.net first of all I add a xml file (emp.xml) from add new item -> add some dummy data into it like
<serverinfo>
<servername>abc</servername>
<serverpwd>123</serverpwd>
</serverinfo>
将文件从应用程序文件夹复制到调试文件夹,并在下面的代码上写以保存数据:-
copy the file from application folder to debug folder and write the following code on for save data :-
Dim path As String = Application.StartupPath & "\emp.xml"
Dim dt As New DataTable("emp")
Dim ds As New DataSet()
ds.ReadXml(path)
dt.Columns.Add(New DataColumn("eid", Type.[GetType]("System.Int32")))
dt.Columns.Add(New DataColumn("ename"))
Dim r As DataRow = ds.Tables(0).NewRow()
r(0) = TextBox1.Text
r(1) = TextBox2.Text
ds.Tables(0).Rows.Add(r)
ds.WriteXml(path)
并将数据显示到名为(dg_Data)的datagridview中
and for display data into datagridview named (dg_Data)
Dim path As String = Application.StartupPath & "\emp.xml"
Dim ds As New DataSet()
ds.ReadXml(path)
dg_Data.DataSource = ds.Tables(0)
但是当它读取xml文件时出现了一些错误,错误是缺少根元素",所以请帮助我..
在此先感谢
Parveen Rathi
But it give some error when it read the xml file the error is "Root element is missing" so please help me..
Thanks in advance
Parveen Rathi
推荐答案
<serverinfos>
<serverinfo>
<servername>abc</servername>
<serverpwd>123</serverpwd>
</serverinfo>
</serverinfos>
这篇关于保存并使用vb.net将数据检索到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!