我想纠正来自sql数据库的数据的.txt文件。我要在.txt文件上写的文本是sql中的XML数据类型。
我正在txt文件中获取xml数据,但它只是一个字符串。我希望它看起来像xml格式。
Dim swRMSRequest As StreamWriter
swRMSRequest = File.CreateText(strFileRMSRequest)
swRMSRequest.WriteLine(dtRow("RMS_reqSentM"))
swRMSRequest.Close()
答案 0 :(得分:0)
由于您只关心单行的XML,因此可以使用ExecuteScalar根据某些条件(例如,PK与值匹配)仅获取RMS_reqSentM
值。获得值后,只需调用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
答案 1 :(得分:0)
您需要将文本文件视为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
答案 2 :(得分:0)
这就是我的工作方式。希望对别人有帮助。
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`