Results 1 to 12 of 12

Thread: [RESOLVED] ASP.net - Reading XML File

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Ottawa
    Posts
    38

    [RESOLVED] ASP.net - Reading XML File

    How would I open and XML file on my "C" drive and read the contents?

    If I save it to a server drive, it's easy:

    Dim myDataSet As New DataSet()
    myDataSet.ReadXml(Server.MapPath("~/XMLData/RightFax.xml"))
    Me.GridView1.DataSource = myDataSet
    GridView1.DataBind()


    What I would prefer to do though, is have the user save the XML file to their PC (the get it via e-mail), but be able to access my web site and load the contents.

    Any ideas would be appreciated
    Last edited by cfisher; Sep 8th, 2009 at 02:58 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ASP.net - Reading XML File

    You can get them to upload the file. Place an ASP.NET FileUpload control on your page.

    If FileUpload1.HasFile Then
    FileUpload1.SaveAs(Server.MapPath("/XMLData/something.xml"))
    End If

    Then read it as you normally would.

    Of course there's more to it than just this. You'd give the XML file a different name for each user if you don't want them to overwrite each other's XML.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Ottawa
    Posts
    38

    Re: ASP.net - Reading XML File

    Thanks for the idea. It seems like a good approach, but I was hoping to avoid transferring it to the server. Is it possible to just open the file from the PC or would I save myself allot of headache just by using your idea?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ASP.net - Reading XML File

    You'll save yourself a lot of headache, because if you go for client-side solution, you're going to have to create an applet - Flash, Java, ActiveX, Silverlight... and the problem with this approach is that you have to write it and then host it, make sure it works with any javascript you might have or with the rest of your app and convince your users to install it (it's almost equivalent to having a desktop app, since all the code's running client side).

    Uploading and downloading a file isn't that difficult, really

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Ottawa
    Posts
    38

    Re: ASP.net - Reading XML File

    That's what I was thinking but was not sure. Thanks for your help. I'm going to go with your first suggestion.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ASP.net - Reading XML File

    Sure, ask us if you get stuck at any point.

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: ASP.net - Reading XML File

    Hey,

    Just to add my two cents...

    In terms of navigating the XML document, XPath is your friend. You will find some links in my signature to help you if you are unsure.

    Gary

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Ottawa
    Posts
    38

    Re: ASP.net - Reading XML File

    Thanks for the help guys. Here is what I ended up doing in hopes it can help others:

    HTML Code:
            If Me.XMLUpload.HasFile = False Then
                Response.Write("<script>alert('You must select a file to load.')</Script>")
                Exit Sub
            End If
    
            If Mid(Me.XMLUpload.PostedFile.FileName, Len(Me.XMLUpload.PostedFile.FileName) - 3, Len(Me.XMLUpload.PostedFile.FileName)) <> ".xml" Then
                Response.Write("<script>alert('Only XML data files can be loaded.')</Script>")
                Exit Sub
            End If
    
            'Get the next sequence number to load into the requests table
            Dim conSeqNo As New OracleClient.OracleConnection("myconn string")
            Dim cmdSeqNo As New OracleClient.OracleCommand("SELECT RMANUMBER.NEXTVAL FROM DUAL", conSeqNo)
            conSeqNo.Open()
            Dim rdrSeqNo As OracleClient.OracleDataReader = cmdSeqNo.ExecuteReader(CommandBehavior.CloseConnection)
            While rdrSeqNo.Read
                Session("RequestID") = rdrSeqNo.GetValue(0)
            End While
            'Implicitly closes the connection because CommandBehaviour.CloseConnection was specified
            rdrSeqNo.Close()
    
            'Move the file to the server
            If Me.XMLUpload.HasFile Then
                Me.XMLUpload.SaveAs(Server.MapPath("~/XMLData/" & Session("RequestID") & ".xml"))
            End If
    
            'Display the file on the screen
            Dim myDataSet As New DataSet()
            myDataSet.ReadXml(Server.MapPath("~/XMLData/" & Session("RequestID") & ".xml"))
            Me.GridView1.DataSource = myDataSet
            GridView1.DataBind()
    
            File.Delete(Server.MapPath("~/XMLData/" & Session("RequestID") & ".xml"))
            Me.GridView1.Visible = False
    
    
    Then in the GridView Row Data Bound event
    HTML Code:
            If e.Row.RowType = DataControlRowType.DataRow Then
                Me.txtCustomer.Text = e.Row.Cells(0).Text
            End If
    So far it works like a charm. Thanks again for you help.

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] ASP.net - Reading XML File

    Hey,

    I just have a couple comments on the code that you have posted, nothing major, but just things to keep in mind.

    1) Try to avoid using Response.Write, this just causing more problems than it's worth. There, have a label on the page, and write the error text to that.
    2) Rather than explicitly calling .Close on the DataReader, I would recommend that you wrap all of those types of objects with in a Using Statement, that way, when the object is about to go out of scope, it is closed and disposed of. You can find an example of this here:

    http://www.vbforums.com/showthread.php?t=469872

    Gary

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] ASP.net - Reading XML File

    For example, that first Response.Write can be replaced with a ClientScript.RegisterStartupScript which is cleaner.

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] ASP.net - Reading XML File

    Ah... yeah, I didn't actually look at what was in the Response.Write. I assumed it was just a string the OP was writing to the response.

    ClientScript.RegisterStartupScript is definitely the way to go.

    Gary

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2001
    Location
    Ottawa
    Posts
    38

    Re: [RESOLVED] ASP.net - Reading XML File

    Thanks for the tips guys, I'll be sure to use them. I'm always looking for ways to write code better and your ideas are a great help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width