Hello, i'm a beginner and i'm creating a guestbook in asp.net with XML.

i have the following:

my aspx page:

Name: <%#DataBinder.Eval(Container.DataItem, "name")%><br />
    E-mail: <a href="mailto:<%# DataBinder.Eval(Container.DataItem, "email") %>"><%# DataBinder.Eval(Container.DataItem, "email") %></a><br />
    Location: <%# DataBinder.Eval(Container.DataItem, "location") %><br />
    Date: <%# DataBinder.Eval(Container.DataItem, "date") %><br />
    <i><%# DataBinder.Eval(Container.DataItem, "entry_Text") %></i>

my code page:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myXmlReader As XmlTextReader = New XmlTextReader(Server.MapPath("Guestbook.xml"))
        Dim mydataset As DataSet = New DataSet
        mydataset.ReadXml(myXmlReader)
        myXmlReader.Close()

        Guestbook.DataSource = mydataset.Tables
        Guestbook.DataBind()

    End Sub

my xml looks like this:

<guestbook>
  <entry name="xxx" email="xxx" location="xxx" date="xxx"></entry>
</guestbook>

Recommended Answers

All 11 Replies

Do you have a question? Just a quick question but why would you want to store data in XML versus a database?

i tried it with a database :) and that wouldn't work,

but i have the code stored somewhere

Ok...first what is Guestbook? I see you bind your datasource to it. Is this a datagrid?

no idea actually, found it on internet this codxe

OK. Well what are you using for your development environment? VS2005, VS2008 ?????

OK....well not sure what you grabbed from the web but I'd use the GridView to display data. It will look nice and as a newby you will learn this pretty easy.

I am going to attach a working version of what you sent me but I changed it to use the gridview control.

I hope you enjoy learning. You will need to use Google because it has some great information.

ok, that helped me a lot, i'm now writing data into my xml but i seem to not add date but replace the first entry, aniy idea?

post your code again so I can see what you're doing.

Protected Sub Post_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Post.Click

        Dim enc As Encoding
        'Create file, overwrite if exists
        'enc is encoding object required by constructor
        'It is null, so default encoding is used
        Dim objXMLTW As New XmlTextWriter(Server.MapPath("guestbook.xml"), enc)
        objXMLTW.WriteStartDocument()
        'Top level (Parent element)
        objXMLTW.WriteStartElement("entry")

        'Child elements, from request form
        objXMLTW.WriteStartElement("Name")
        objXMLTW.WriteString(Request("Name"))
        objXMLTW.WriteEndElement()

        objXMLTW.WriteStartElement("Email")
        objXMLTW.WriteString(Request("email"))
        objXMLTW.WriteEndElement()

        objXMLTW.WriteStartElement("website")
        objXMLTW.WriteString(Request("website"))
        objXMLTW.WriteEndElement()


        objXMLTW.WriteStartElement("Bericht")
        objXMLTW.WriteString(Request("message"))
        objXMLTW.WriteEndElement()


        objXMLTW.WriteEndElement() 'End top level element
        objXMLTW.WriteEndDocument() 'End Document
        objXMLTW.Flush() 'Write to file
        objXMLTW.Close()
        'Display File Just Created
        'ReadXML(Server.MapPath("applicant.xml"))

    End Sub

<entry>
  <Name>test</Name><Email>test</Email><website>test</website><Bericht>test</Bericht>
</entry>

Maybe this would work better for you.

Dim ds As New DataSet
        Dim dr As DataRow
        'read you xml file into a dataset
        ds.ReadXml(Server.MapPath("Guestbook.xml"))
        'create a new row for your new guest entry
        dr = ds.Tables(0).NewRow
        'enter the correct data to each column of your new row
        dr("name") = "name3"
        dr("email") = "email3"
        dr("location") = "location3"
        dr("date") = "date3"
        'add the new row to your dataset
        ds.Tables(0).Rows.Add(dr)
        'write your new xml with appended row back to the file system
        ds.WriteXml(Server.MapPath("Guestbook2.xml"))

I read the current xml file into a dataset. I add a new row to the dataset that contains all your previous entries. I add the information to the new row. I then write that dataset back to the file system. One good thing about this is that it also formats your xml properly.

I am sure there are other ways to handle this but I've found this easiest for a beginner.

One other advantage of this is that you can also change the data while you have it in the dataset. This allows you to modify, delete or add very easily.

Let me know if this helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.