[英]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.