0

我有多个 SQL 服务器表,我想从中提取数据并写入 XML。我希望我的 XML 形成如下:

 <Data>
     <Query1Table>
          <Table>
              <Column1>Data</Column1>
              <Column2>Data</Column2>
              ...
          </Table>
      </Query1Table>
      <Query2Table>
          <Table>
              <Column1>Data</Column1>
              <Column2>Data</Column2>
              ...
          </Table>
      </Query2Table>
   </Data>

我正在使用数据集来编写 xml,但我正在使用的代码不会附加数据,它会覆盖:

        Dim connetionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim directory As String
        Dim ds As New DataSet
        Dim sql As String

        connection = New SqlConnection(connetionString)
        sql = "select * from scheduledata"
        connection.Open()
        adapter = New SqlDataAdapter(sql, connection)
        adapter.Fill(ds)

        ds.DataSetName = "Schedule"
        ds.WriteXml(directory)

        ds.Clear()
        sql = "select * from costdata"
        adapter = New SqlDataAdapter(sql, connection)
        adapter.Fill(ds)
        ds.WriteXml(directory)

我曾尝试通过一次调用 SQL 查询将它们全部添加到同一个数据集中,但这无助于在 XML 中将它们分开——它将它们分组在同一个节点中。

如果有人有好的建议,我愿意接受不同的方法。

4

2 回答 2

1

您已定义directory为字符串,因此ds.WriteXml将(创建和)写入该名称的文件。

使用FileStreamorXmlTextWriter代替。

于 2013-07-18T17:35:06.057 回答
0

我写入 .xml 文件(有效)的方式如下

Private Sub AuthenticationContinuebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuthenticationContinuebtn.Click

    On Error GoTo PasswordHandler

    GlobalVariables.Username = Usernametxtbx.Text
    GlobalVariables.Password = Passwordtxtbx.Text
    GetUsernamePassword()
    Close()
    Exit Sub
    PasswordHandler:
    MsgBox("Incorrect password or username.")
End Sub


Private Sub UserAuthenticationWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim FileName4 As String = "C:\Forte\UsernamePassword.xml"
    Dim FileRead4 As XmlTextReader = New XmlTextReader("C:\Forte\UsernamePassword.xml")

    'If statement to see if file exists.
    If System.IO.File.Exists(FileName4) = True Then

        Do While (FileRead4.Read)
            Select Case FileRead4.NodeType

                Case XmlNodeType.Text, XmlNodeType.Element

                    If FileRead4.Name = "Username" Then
                        FileRead4.Read()
                        Usernametxtbx.Text = FileRead4.Value
                        GlobalVariables.Username = Usernametxtbx.Text
                    End If
                    If FileRead4.Name = "Password" Then
                        FileRead4.Read()
                        Passwordtxtbx.Text = FileRead4.Value
                        GlobalVariables.Password = Passwordtxtbx.Text
                    End If
            End Select
        Loop
    Else
        MainBox.MainTextBox.AppendText(FileName4 & " could not be found. Settings are restored to defaults.")
        MainBox.Logging(Date.Now & FileName4 & " could not be found. Settings are restored to defaults.")
    End If
    FileRead4.Close()
End Sub

Public Sub createNode3(ByVal Username As String, ByVal Password As String, ByVal writer As XmlTextWriter)

    'On Error Resume Next
    writer.WriteStartElement("Username_Password")
    writer.WriteStartElement("Username")
    writer.WriteString(Username)
    writer.WriteEndElement()
    writer.WriteStartElement("Password")
    writer.WriteString(Password)
    writer.WriteEndElement()
    writer.WriteEndElement()

End Sub
于 2013-07-18T17:42:48.010 回答