1.写一个保存xml文件到本地文件的工具类
Imports System.IO
Imports System.Xml
Public Class SaveXmlUtil
Public Shared Function SaveXmlToLocalhost(ByVal obj As XmlDocument, ByVal filePath As String, ByVal fileName As String) As String
Try
If IsNothing(obj) Then
Return String.Empty
End If
Dim xmlString As String = ""
filePath = filePath & "\\" & System.DateTime.Now.ToString("yyyy-MM-dd")
If Directory.Exists(filePath) = False Then
My.Computer.FileSystem.CreateDirectory(filePath)
End If
Try
Dim savePath As String = Path.Combine(filePath, fileName)
obj.Save(savePath)
Catch ex As Exception
Return String.Empty
End Try
Return obj.OuterXml.ToString
Catch ex As Exception
Return String.Empty
End Try
End Function
End Class
2.外部调用工具类
Imports System.Xml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fileName As String = "NewText" + ".xml"
Dim filePath As String = "D:\Hao\xml"
Dim docXml As New XmlDocument
Dim rsData As String = SaveXmlUtil.SaveXmlToLocalhost(docXml, filePath, fileName)
End Sub
End Class