[英]Trying to delete an XML file throws “The process cannot access the file…” error
[英]Error trying to download an XML file
我正在嘗試下載XML文件,該文件已存在於指定的路徑中,我對VB不熟悉,並且此代碼可能不正確 ,我需要幫助才能下載現有的xml文件,這里是代碼 :
Protected Sub DownloadFile(ByVal sPath As String)
Dim TargetFile As New System.IO.FileInfo(sPath)
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" +
TargetFile.Name)
Response.AddHeader("Content-Length", TargetFile.Length.ToString())
Response.ContentType = "text/xml"
Response.WriteFile(TargetFile.FullName)
Response.End()
End Sub
控制台中返回的錯誤 :
未捕獲的錯誤: Sys.WebForms.PageRequestManagerParserErrorException :無法解析從服務器收到的消息。 導致此錯誤的常見原因是通過調用Response.Write(),響應過濾器,HttpModules或服務器跟蹤來修改響應。
使問題具體化 :
我已經序列化了一個對象並創建了一個XML文件,然后我只想下載該文件,我的困難是下載該文件。
Dim oObj1 As New System.Xml.Serialization.XmlSerializer(GetType(eSocial.Eventos.evtTabHorTur.eSocial))
Dim sFileName = Date.Now.ToString("yyyyMMddHHmmss") & ".xml"
Dim sPath = Constantes.Ambiente.CaminhoSite & "temp\" & sFileName
Dim oFile As New System.IO.StreamWriter(sPath)
oObj1.Serialize(oFile, eSocialCamposXml)
oFile.Close()
您是在說下載有困難,但是代碼中除了顯示先寫入文件然后序列化文件外沒有任何內容。 您將使用“ StreamReader”或類似方式來讀取文件。 這是一個簡單的例子。 假設我在文件位置具有xml結構,其架構如下所示:
<root>
<test>Data</test>
</root>
我可以在VB.NET中編寫此代碼以獲得它:
Sub Main()
Dim xmlFile As XDocument
Dim fileLocation = "D:\\Test Code\\Test.xml"
Using sr = New StreamReader(fileLocation)
xmlFile = XDocument.Parse(sr.ReadToEnd())
End Using
Console.WriteLine(xmlFile.Root.Element("test").Value.ToString)
Console.ReadLine()
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.