Getting an XML file served from aspx

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gchq
    New Member
    • Jan 2007
    • 96

    Getting an XML file served from aspx

    Hi there

    The issue

    Need to bring in an XML file to the App that is not in the App root.

    Part Solution

    I have built an aspx page that will bring in the XML file and 'serve' it

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim GetFile As String
            GetFile = "\\Interclaims1\InterclaimsC\Data\Shared\XML\Parkwood\Newsletter.xml"
            If New System.IO.FileInfo(GetFile).Exists Then
                Response.Clear()
                Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
                Response.ContentType = "text/xml"
                Response.ContentEncoding = Encoding.UTF8
                Response.WriteFile(GetFile)
                Response.End()
            End If
    
        End Sub
    This runs without a problem and shows the XML content

    Now I need to to use xml events to get a value from that XML file

    Code:
    Dim xmldoc As New XmlDataDocument
                Dim vXmlNode As XmlNode
                xmldoc.Load(Server.MapPath(vmapxmlpath))
                Dim xmlnodelist As XmlNodeList = xmldoc.DocumentElement.SelectNodes("NewsLetter_Data")
                For Each vXmlNode In xmlnodelist
                    Dim vType As Integer = vXmlNode.SelectSingleNode("Newsletter_ID").InnerText
                    If vType = 2 Then
                        Dim vReplaceText As New ReplaceText
                        Dim vConverted As String = vReplaceText.ReturnHTML(vXmlNode("NewsLetter_Body").InnerText)
                        TextBox1.Text = vConverted
                    End If
                Next
    Sooooooo the question is how do I create a valid MapPath so that it loads the document correctly?

    Dim vmapxmlPath as string = "ServeXML.a spx"

    causes an error = it needs to resolve the url to get the file, but I can't figure out the correct syntax....
  • gchq
    New Member
    • Jan 2007
    • 96

    #2
    Think I have found a way round this - to set up a local temp directory, then write a class to get the original xml document and save it to the local temp dir - this can then be accessed by the page that wants to select, insert or update changes to it, then call the class again to move the file...

    Here is the class

    Code:
    Public Sub GetXmlFile()
            Dim GetFile As String
            GetFile = "\\Interclaims1\InterclaimsC\Data\Shared\XML\Parkwood\Newsletter.xml"
            If New System.IO.FileInfo(GetFile).Exists Then
    
                Dim vPath As String = HttpContext.Current.Server.MapPath("..\XML\XML_Temp\Newsletter_Temp2.xml")
                Dim objFile As System.IO.File
                Dim strSource As String = GetFile
                Dim strDest As String = vPath
                If objFile.Exists(strDest) Then
                    objFile.Delete(strDest)
                End If
                objFile.Copy(strSource, strDest)
            End If
        End Sub
    
        Public Sub SaveFile()
            Dim vPath As String = HttpContext.Current.Server.MapPath("..\XML\XML_Temp\Newsletter_Temp2.xml")
            If New System.IO.FileInfo(vPath).Exists Then
                'copy the file back to the central location
                Dim objFile As System.IO.File
                Dim strSource As String = vPath
                Dim strDest As String = "\\Interclaims1\InterclaimsC\Data\Shared\XML\Parkwood\Newsletter.xml"
                Dim strBackup As String = "\\Interclaims1\InterclaimsC\Data\Shared\XML\Parkwood\Newsletter_Backup.xml"
                If objFile.Exists(strBackup) Then
                    objFile.Delete(strBackup)
                End If
                objFile.Copy(strDest, strBackup)
                If objFile.Exists(strDest) Then
                    objFile.Delete(strDest)
                End If
                objFile.Move(strSource, strDest)
            End If
    
        End Sub
    Bit long winded, but it does work....

    Comment

    Working...