【发布时间】:2022-01-04 07:54:04
【问题描述】:
我想将 SQL 数据库中的数据写入 XML 文件。我知道可以使用数据集,但我不想使用它,因为我想用 XmlTextWriter 格式化我自己的 XML 文件。
我会给你一些参考。
使用我的连接字符串(名称、密码...),您可以构建一个新的 SqlConnection。然后我建立一个字符串 我的 SQL 查询在哪里。然后我打开连接,它可以工作。但我的问题是,我不知道如何在我的 XML 文件中写入查询的值。
connection = New SqlConnection(connetionString)
SQL 查询
Dim city As String = "SELECT City FROM info WHERE No = '1'"
编写我如何构建 XML 文件的代码。
Dim xmlfile As String = "path+name"
If IO.File.Exists(xmlfile) = True Then
IO.File.Delete(xmlfile)
End If
Dim enc As New System.Text.UTF8Encoding
Dim XMLbg As New Xml.XmlTextWriter(xmlfile, enc)
With XMLbg
.Formatting = Xml.Formatting.Indented
.Indentation = 4
.WriteStartDocument()
.WriteStartElement("Data")
--------------------------------------------------------
.WriteElementString("City", **'here must be the Data for the City'** )
.WriteEndElement() 'Data
'--------------------------------------------------------
XMLbg.Close()
End With
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Exception ", MessageBoxButtons.OK, MessageBoxIcon.Error)
也许有人知道怎么做。谢谢你:)
【问题讨论】: