【问题标题】:How to modify, delete and add to xml that is in a dataset (visual basic)如何修改、删除和添加到数据集中的 xml (visual basic)
【发布时间】:2016-03-24 05:31:15
【问题描述】:

我正在制作一个通讯录,用户必须能够添加、更新和删除通讯录的条目。我正在使用数据集和 datagridview 来显示我正在使用的 xml 文件,并且我得到了要显示的联系人,但现在我需要能够添加、更新(即更改名字、姓氏电子邮件和电话号码) 并删除联系人。

这是我目前的代码:

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DataSet1.ReadXml("contactlist.xml")
        DataGridView1.DataSource = DataSet1
        DataGridView1.DataMember = "student"
    End Sub
End Class

这是我的xml

<?xml version="1.0" encoding="UTF-8"?>
<contact>
	<student>
		<FirstName>
		John
		</FirstName>
		<LastName>
		Smith
		</LastName>
		<Email>
		kooldudesmith@gmail.com
		</Email>
		<Number>
		905-453-9220
		</Number>
	</student>
	<student>
		<FirstName>
		Joanna
		</FirstName>
		<LastName>
		Smith
		</LastName>
		<Email>
		joannasupermail@hotmail.com
		</Email>
		<Number>
		905-453-8767
		</Number>
	</student>
	<student>
		<FirstName>
		Jaime
		</FirstName>
		<LastName>
		Smith
		</LastName>
		<Email>
		little_cat_mouse@yahoo.com
		</Email>
		<Number>
		905-432-1232
		</Number>
	</student>
	<student>
		<FirstName>
		Bill
		</FirstName>
		<LastName>
		Pang
		</LastName>
		<Email>
		bill.pang7@gmail.com
		</Email>
		<Number>
		647-789-123
		</Number>
	</student>
	<student>
		<FirstName>
		William
		</FirstName>
		<LastName>
		Nguyen
		</LastName>
		<Email>
		ovi8.washington@gmail.com
		</Email>
		<Number>
		647-963-6531
		</Number>
	</student>
</contact>

【问题讨论】:

  • 您有什么具体问题吗?
  • 基本上,我需要找到一种直接从应用程序修改 xml 文件的方法。我已经尝试查看其他人提出的问题,但我无法让这些技术发挥作用。

标签: xml vb.net datagridview dataset


【解决方案1】:

使用普通的 DataTable 方法,保存模式看起来像这样,我们设置了 xml 标题和数据集名称(确保也为表命名,在这种情况下我将其命名为 Product)

Using ds As New DataSet
    ' make sure to have dt table name set
    ds.DataSetName = "Products"
    ds.Tables.Add(dt)
    Dim stream As New XmlTextWriter(IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.xml"), Encoding.UTF8)
    stream.WriteStartDocument()
    ds.WriteXml(stream)
    stream.Close()
End Using

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2019-02-03
    • 2011-10-17
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多