【问题标题】:writing xml data into .txt file将 xml 数据写入 .txt 文件
【发布时间】:2020-04-13 15:04:35
【问题描述】:

我想纠正数据来自 sql 数据库的 .txt 文件。我想在 .txt 文件上写的文本是 sql 中的 XML 数据类型。

我在 txt 文件中获取 xml 数据,但它充当单个字符串。我希望它看起来像 xml 格式。

 Dim swRMSRequest As StreamWriter
 swRMSRequest = File.CreateText(strFileRMSRequest)
 swRMSRequest.WriteLine(dtRow("RMS_reqSentM"))
 swRMSRequest.Close()

文件路径为strFileRMSRequestdtRow("RMS_reqSentM") 如下所示:

我希望我的文本文件看起来像

【问题讨论】:

标签: xml vb.net text


【解决方案1】:

您需要将文本文件视为 XML。 LINQ to XML 是最好的 API。

VB.NET

Sub Main
    Dim strXMLFile As String = "e:\temp\temp.xml"
    Dim xmlDoc As XDocument = XDocument.Parse(dtRow("RMS_reqSentM").ToString)

    Dim Settings As New XmlWriterSettings()
    Settings.Indent = True            ' make it False if you want XML as one single line
    Settings.OmitXmlDeclaration = True  ' Suppress the xml header <?xml version="1.0" encoding="utf-8"?>
    Settings.Encoding = New System.Text.UTF8Encoding(False) ' The false means, do not emit the BOM.

    Dim Writer As XmlWriter = XmlWriter.Create(strXMLFile, Settings)

    xmlDoc.Save(Writer)
End Sub

【讨论】:

  • 它正在创建空白 .xml 文件
  • XDocument.Parse() 调用需要将其参数作为字符串。所以dtRow("RMS_reqSentM").ToString 可能会有所帮助。
【解决方案2】:

由于您只关心单行的 XML,因此使用 ExecuteScalar 根据某些条件仅获取您的 RMS_reqSentM 值(我的示例将是 PK 匹配值的位置)。获得值后,只需调用 IO.File.WriteAllText 将值写入文本文件。

'Declare the object that will be returned from the command
Dim xml As String

'Declare the connection object
Dim con As OleDbConnection

'Wrap code in Try/Catch
Try
    'Set the connection object to a new instance
    con = New SqlConnection()

    'Create a new instance of the command object
    'TODO: Change MyTable and MyTableId to valid values
    Using cmd As SqlCommand = New SqlCommand("SELECT RMS_reqSentM FROM MyTable WHERE MyTableId = @0;", con)
        'Paramterize the query
        'TODO: Change MyId to a valid value
        cmd.Parameters.AddWithValue("@0", MyId)

        'Open the connection
        con.Open()

        'Use ExecuteScalar to return a single value
        xml = cmd.ExecuteScalar()

        'Close the connection
        con.Close()
    End Using
Catch ex As Exception
    'Display the error
    Console.WriteLine(ex.Message)
Finally
    'Check if the connection object was initialized
    If con IsNot Nothing Then
        If con.State = ConnectionState.Open Then
            'Close the connection if it was left open(exception thrown)
            con.Close()
        End If

        'Dispose of the connection object
        con.Dispose()
    End If
End Try

If (Not String.IsNullOrWhitespace(xml)) Then
    'Write the XML to the text file
    IO.File.WriteAllText(strFileRMSRequest, xml)
End If

【讨论】:

  • 请非常小心使用Parameters.AddWithValue,特别是在你可能有无效类型转换的情况下。总是更好地使用 Parameters.Add("@Name", DbType)
【解决方案3】:

这就是我完成它的方式。希望对其他人有所帮助。

Protected Sub GetTextFiles(ByVal strPath As String, ByVal strmessage As String, 
ByVal reqType As String)


    Try
        If (reqType = "XML") Then
            Dim swRMSRequest As StreamWriter
            Dim txtstr As String = String.Empty
            'swRMSRequest = File.CreateText(strFileRMSRequest)
            swRMSRequest = File.CreateText(strPath)
            Dim xmldoc As XmlDocument = New XmlDocument()
            'xmldoc.LoadXml(dtRow("RMS_reqSentM"))
            xmldoc.LoadXml(strmessage)
            Dim sb As New StringBuilder()
            ''`'We will use stringWriter to push the formated xml into our StringBuilder sb.`  
            Using stringWriter As New StringWriter(sb)
                '' `'We will use the Formatting of our xmlTextWriter to provide our indentation.`  
                Using xmlTextWriter As New XmlTextWriter(stringWriter)
                    xmlTextWriter.Formatting = Formatting.Indented
                    xmldoc.WriteTo(xmlTextWriter)
                End Using
                txtstr = sb.ToString()
            End Using
            swRMSRequest.WriteLine(txtstr)
            swRMSRequest.WriteLine(" ")
            swRMSRequest.Close()
        Else
            Dim swRMSRequest As StreamWriter
            Dim txtstr As String = strmessage
            swRMSRequest = File.CreateText(strPath)
            Dim sb As New StringBuilder()
            swRMSRequest.WriteLine(txtstr)
            swRMSRequest.WriteLine(" ")
            swRMSRequest.Close()
        End If


    Catch ex As Exception
        ex.Message.ToString()
    End Try
End Sub`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多