CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Apr 2004
    Posts
    5

    ASP.NET / XML problem (Writing to an XML file)

    I'm not sure where I should be posting this, if it's more of an ASP problem or an XML problem, so I apologize if it's in the wrong location.

    I'm brand new to using XML and ASP (.NET or otherwise) (in fact this is my first project), so bear with me, I may need a little hand-holding.

    I'm trying to create a page which simply adds data to an existing xml file. Ultimately it will add the data from a form, but to keep things simple, I've been simply using pre-defined values.

    Originally I was able to get the script to write the xml exactly as I needed, however it would simply overwrite the existing file every time it was accessed. This is unacceptable, as it will be used to add data to one file, sort of like a mini database. So after much pain and gnashing of teeth I managed to get the script to append the data to an existing file. However, it wrote the xml wrong! ...I still cannot get it to format the xml properly, and am at my wits end as to how I should fix it.

    If anyone could help me with this, I would be very appreciative!

    Thanks!


    This is how I want my xml to look:
    PHP Code:
    <?xml version="1.0"?>

    <leads>

      <lead>

        <name>mac</name>

        <phone>555-123-4567</phone>

        <email>[email protected]</email>

        <besttimetocall>3pm</besttimetocall>

        <wheretravel>iceland</wheretravel>

        <ticketsneeded>4</ticketsneeded>

        <numbertimestravel>3</numbertimestravel>

      </lead>

      <lead>

        <name>mr jones</name>

        <phone>555-555-5555</phone>

        <email>[email protected]</email>

        <besttimetocall>10am</besttimetocall>

        <wheretravel>bermuda</wheretravel>

        <ticketsneeded>2</ticketsneeded>

        <numbertimestravel>1</numbertimestravel>

      </lead>

    </leads>


    Unfortunately, when I write to the file with my xmlwriter program, it ends up looking like this:
    PHP Code:
    <?xml version="1.0"?>

    <leads>

      <lead />

        <name>moo</name>

        <phone>324982</phone>

        <email>[email protected]</email>

        <besttimetocall>never</besttimetocall>

        <wheretravel>test</wheretravel>

        <ticketsneeded>test</ticketsneeded>

        <numbertimestravel>test</numbertimestravel>

      <lead />

      <name>bubba</name>

      <phone>555-555-5555</phone>

      <email>[email protected]</email>

      <besttimetocall>3pm</besttimetocall>

      <wheretravel>bermuda!</wheretravel>

      <ticketsneeded>2</ticketsneeded>

      <numbertimestravel>1</numbertimestravel>

      <lead />

      <name>bubba</name>

      <phone>555-555-5555</phone>

      <email>[email protected]</email>

      <besttimetocall>3pm</besttimetocall>

      <wheretravel>bermuda!</wheretravel>

      <ticketsneeded>2</ticketsneeded>

      <numbertimestravel>1</numbertimestravel>

    </leads>


    If you'll notice, the tags are not falling into the <lead> </lead> tags like I want them to. Below follows the script that writes the data:


    PHP Code:
    <%@ Page Language="VB" debug="true" %>

    <%@ 
    Import Namespace="System.Xml" %>

    <
    script runat=server>

        
    sub Page_Load(obj as objectas eventargs)

              
    dim xmldoc as new XMLDocument()

            
    dim strOutput as string ""

            
    try

                
    xmldoc.Load(Server.MapPath("leads2.xml"))

                    
    dim eleLead as XmlElement xmldoc.CreateElement ("lead")

                    

                    
    dim eleName as XmlElement xmldoc.CreateElement ("name")

                        
    dim eleNameText as XmlText xmldoc.CreateTextNode("bubba")

                    

                    
    dim elePhone as XmlElement xmldoc.CreateElement ("phone")

                        
    dim elePhoneText as XmlText xmldoc.CreateTextNode("555-555-5555")

                    

                    
    dim eleEmail as XmlElement xmldoc.CreateElement ("email")

                        
    dim eleEmailText as XmlText xmldoc.CreateTextNode("[email protected]")

                    

                    
    dim eleBestTimeToCall as XmlElement xmldoc.CreateElement ("besttimetocall")

                        
    dim eleBestTimeToCallText as XmlText xmldoc.CreateTextNode("3pm")

                    

                    
    dim eleWhereTravel as XmlElement xmldoc.CreateElement ("wheretravel")

                        
    dim eleWhereTravelText as XmlText xmldoc.CreateTextNode("bermuda!")

                        

                    
    dim eleTicketsNeeded as XmlElement xmldoc.CreateElement ("ticketsneeded")

                        
    dim eleTicketsNeededText as XmlText xmldoc.CreateTextNode("2")

                        

                    
    dim eleNumberTimesTravel as XmlElement xmldoc.CreateElement ("numbertimestravel")

                        
    dim eleNumberTimesTravelText as XmlText xmldoc.CreateTextNode("1")

                    

                    

                    

                    
    xmldoc.DocumentElement.AppendChild(eleLead)

                    
    xmldoc.DocumentElement.AppendChild(eleName)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(eleNameText)

                    
    xmldoc.DocumentElement.AppendChild(elePhone)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(elePhoneText)

                    
    xmldoc.DocumentElement.AppendChild(eleEmail)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(eleEmailText)

                    
    xmldoc.DocumentElement.AppendChild(eleBestTimeToCall)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(eleBestTimeToCallText)

                    
    xmldoc.DocumentElement.AppendChild(eleWhereTravel)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(eleWhereTravelText)

                    
    xmldoc.DocumentElement.AppendChild(eleTicketsNeeded)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(eleTicketsNeededText)

                    
    xmldoc.DocumentElement.AppendChild(eleNumberTimesTravel)

                        
    xmldoc.DocumentElement.LastChild.AppendChild(eleNumberTimesTravelText)



                    
    xmldoc.Save(Server.MapPath("leads2.xml"))

                    

                catch 
    ex as Exception

                    strOutput 
    "Error accessing XML file"

                    

                
    end try



                
    output.Text "Append operation successful"

    End Sub



    </script>



    <
    html><body>

        <
    asp:Label id="output" runat="server" />

    </
    body>

    </
    html

    Any idea how I get the information to properly fall inside the <lead> </lead> tags?



    Thanks!
    Last edited by dotNetDave; April 19th, 2004 at 12:24 PM.

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

Featured