updating with aspx and XML file

S

slinky

Thanks in advance fo rany help... I have an XML data file (well-
formed) that I need to place into my website's app_data folder. I
would like to have an .aspx form on my site that simply has two
textboxes (corresponding to two XML fields in my file), and a
"submit"
button that will update the file. Any clues as to the code to use?

I'm most familiar with the DataAdapter/DataSet model and using
SQLserver files and having an INSERT INTO query to update. Can this
be
used on an XML file also? There may be better ways, but just curious
if this model COULD work with XML.
Thanks!
 
G

Guest

Thanks in advance fo rany help... I have an XML data file (well-
formed) that I need to place into my website's app_data folder. I
would like to have an .aspx form on my site that simply has two
textboxes (corresponding to two XML fields in my file), and a
"submit"
button that will update the file. Any clues as to the code to use?

I'm most familiar with the DataAdapter/DataSet model and using
SQLserver files and having an INSERT INTO query to update. Can this
be
used on an XML file also? There may be better ways, but just curious
if this model COULD work with XML.
Thanks!

Dim myDataSet as New DataSet()
myDataSet.ReadXml(Server.MapPath("books.xml"))

http://aspnet.4guysfromrolla.com/articles/052902-1.aspx

http://www.google.com/search?hl=en&q=DataSet+xml+asp.net
 
S

slinky

Thanks! http://aspnet.4guysfromrolla.com/articles/052902-1.aspx is a
great site...but I tried out some of you code below from the site (I'm
a student-newbie using Visual Web Developer Express 2005). I pasted
the following into a blank .aspx page and your books.xml file into my
app_data folder on my site, re-build, and get rid of some errors, I
try to view it in my browser and I get server errors:

here's my aspx:

<%@ import Namespace="System.Data" %>
<script runat="server">
sub Page_Load(sender as Object, e as EventArgs)
Dim myDataSet as New DataSet()

myDataSet.ReadXml(Server.MapPath("books.xml"))

dgBooks.DataSource = myDataSet
dgBooks.DataBind()

dgBooksPretty.DataSource = myDataSet
dgBooksPretty.DataBind()
end sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>StartPage</title>
</head>
<body>
<b>The Contents of the XML File
<a href="/demos/books.xml"><code>books.xml</code></a></b><br />
<asp:datagrid id="dgBooks" runat="server" />

<p align="center">
<b>A Nicer Looking DataGrid Representation of the XML File
<a href="/demos/books.xml"><code>books.xml</code></a></b><br />
</p>
<asp:datagrid id="dgBooksPretty" runat="server"
AutoGenerateColumns="False"
Font-Name="Verdana"
Font-Size="Small"
HorizontalAlign="Center"
ItemStyle-BackColor="#FFFFCC"
AlternatingItemStyle-BackColor="#EEEEEE">

<HeaderStyle BackColor="Red" HorizontalAlign="Center"
ForeColor="White" Font-Bold="True" />

<Columns>
<asp:BoundColumn HeaderText="Title" DataField="title" />
<asp:BoundColumn HeaderText="Author" DataField="author" />
<asp:BoundColumn HeaderText="Year" DataField="year" />
</Columns>
</asp:datagrid>

</body>
</html>

and here's my xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<books>
- <book>
<title>Teach Yourself Active Server Pages 3.0 in 21 Days</title>
<author>Mitchell</author>
<year>1999</year>
</book>
- <book>
<title>Designing Active Server Pages</title>
<author>Mitchell</author>
<year>2000</year>
</book>
- <book>
<title>ASP.NET: Tips, Tutorials, and Code</title>
<author>Mitchell</author>
<year>2001</year>
</book>
- <book>
<title>ASP Unleashed</title>
<author>Walther</author>
<year>1998</year>
</book>
- <book>
<title>ASP.NET Unleashed</title>
<author>Walther</author>
<year>2002</year>
</book>
- <book>
<title>Creating Data Driven ASP.NET Applications</title>
<author>Seven</author>
<year>2002</year>
</book>
</books>
 
S

slinky

I figured it out. I set the web.config file to <customErrors
mode="Off"/>
then noticed the error saying the app could not find the xml file
because I had it in my App_Data folder.
So I moved it to the remote host folder and it works fine now. Thanks
for helping me up the steep hill
of understanding reading XML files as data into my apps.

BTW... since (correct me if I'm wrong) ALL data is ultimately sent
over the internet as XML files,
is there an advantage in using them as your relational database to
start with? I know the markup
makes them rather verbose, but would not in reality a SQLserver file
be turned into an XML file
for moving over the net?... Kind of an academic question, just
curious... Thanks!
 
G

Guest

BTW... since (correct me if I'm wrong) ALL data is ultimately sent
over the internet as XML files,
is there an advantage in using them as your relational database to
start with? I know the markup
makes them rather verbose, but would not in reality a SQLserver file
be turned into an XML file
for moving over the net?... Kind of an academic question, just
curious... Thanks!

All data sent over the internet are sent in the binary packets :)

XML can be useful a number of reasons: easy to edit, lightweight, no
additional license costs and so on. Database is secure, easy to
search, efficient for large data sets...

http://www.google.com/search?q=xml+vs+database
 
S

slinky

That makes sense...... also along the lines of XML editing, etc. If I
had an .aspx form that I wanted to simply have three textboxes
(corresponding to Author, Title, and Year as in your XML file), and a
submit button... how could I setup the code/form so the user could
enter a book and hit Submit and add that to the XML file? I don't want
the list of books to be displayed though on this form. I've done this
before in vb.net with an XML file for a desktop app, but not for
an .aspx app. Any clues? Thanks!
 
G

Guest

That makes sense...... also along the lines of XML editing, etc. If I
had an .aspx form that I wanted to simply have three textboxes
(corresponding to Author, Title, and Year as in your XML file), and a
submit button... how could I setup the code/form so the user could
enter a book and hit Submit and add that to the XML file? I don't want
the list of books to be displayed though on this form. I've done this
before in vb.net with an XML file for a desktop app, but not for
an .aspx app. Any clues? Thanks!


There are tons of articles and examples about that subject on the web

For example: http://www.freevbcode.com/ShowCode.asp?ID=2789
 
S

slinky

I went to the site you listed for Writing to XML... I have put
together a form with a textbox and a button... my code is below... I
simply want a user to add a record to the XML file then hit the submit
button, but I can seem to get the plumbing right on this one. I get
several errors. I've been able to do not unsimilar things with XML for
desktop apps (see my code at very bottom) but ASP is really throwing
me! Thanks for any help... struggling still....

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub btnWriteXML_OnClick(ByVal sender As Object, ByVal e As
EventArgs)
Try
Dim enc As Encoding
Dim objXMLTW As New
HtmlTextWriter(Server.MapPath("menu.xml"), enc)
objXMLTW.WriteStartDocument()
objXMLTW.WriteStartElement("Name")
objXMLTW.WriteString(Request("txtName"))
objXMLTW.WriteEndElement()
objXMLTW.WriteEndDocument()
objXMLTW.Flush()
objXMLTW.Close()
Catch Ex As Exception
Dim MessageBox As MsgBoxResult
MessageBox = "The following error occurred: " & Ex.Message
End Try
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnWriteXML" runat="server" Text="Button" />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></div>
</form>
</body>
</html>


There are tons of articles and examples about that subject on the web

For example:http://www.freevbcode.com/ShowCode.asp?ID=2789

__________________________________________________________________
Imports System.Data
Imports System.Xml
Public Class StepByStep2_4
Dim xdd As XmlDataDocument
Dim ds As DataSet
Private Sub btnLoadXml_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnLoadXml.Click
Dim xtr As XmlTextReader = _
New XmlTextReader("C:\Documents and Settings\dcampbe\My
Documents\Visual Studio 2005\Projects\310C02\310C02\Books.xml")
xdd = New XmlDataDocument()
ds = xdd.DataSet()
ds.ReadXmlSchema(xtr)
xtr.Close()
xtr = New XmlTextReader("C:\Documents and Settings\dcampbe\My
Documents\Visual Studio 2005\Projects\310C02\310C02\Books.xml")
xtr.WhitespaceHandling = WhitespaceHandling.None
xdd.Load(xtr)
dgXML.DataSource = ds
dgXML.DataMember = "Book"
xtr.Close()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSave.Click
Dim xtw As XmlTextWriter = New XmlTextWriter("C:\Documents and
Settings\dcampbe\My Documents\Visual Studio 2005\Projects
\310C02\310C02\Books.xml", System.Text.Encoding.UTF8)
xtw.Formatting = Formatting.Indented
xdd.WriteTo(xtw)
'Clean up
xtw.Close()
MessageBox.Show("The XML file has been successfully
updated !")
End Sub
End Class
 
G

Guest

I went to the site you listed for Writing to XML... I have put
together a form with a textbox and a button... my code is below... I
simply want a user to add a record to the XML file then hit the submit
button, but I can seem to get the plumbing right on this one. I get
several errors. I've been able to do not unsimilar things with XML for
desktop apps (see my code at very bottom) but ASP is really throwing
me! Thanks for any help... struggling still....

Open the example I've sent you.

Where did you found a HtmlTextWriter?

Change it to XMLTextWriter.

Add a reference to System.XML namespace

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

and add an OnClick() event

<asp:Button ID="btnWriteXML" runat="server" Text="Button"
OnClick="btnWriteXML_OnClick" />
 
S

slinky

In that example where is the XML file Applicant.xml in the
downloaded...all I saw was menu.xml
 
G

Guest

In that example where is the XML file Applicant.xml in the
downloaded...all I saw was menu.xml

Brian, it contans two files: menu.xml and XML_DEMO.aspx

XML_DEMO.aspx has an example of the code
 
S

slinky

The following error occurred: Access to the path 'E:\kunden\homepages
\26\d190091667\applicant.xml' is denied. I have the XML file in my
correct place on the tree, and I can view its contents.
 
G

Guest

The following error occurred: Access to the path 'E:\kunden\homepages
\26\d190091667\applicant.xml' is denied. I have the XML file in my
correct place on the tree, and I can view its contents.

'Access to the path is denied' means that your application has no
access rights to update the file. To fix this, you can either add
write permissions to ASPNET account, or move the xml file to a
directory where you have a write access.

Hope it helps
 
S

slinky

How can I check the security of the XML file or change it or move it?
I can view it in a browser and can display it in the (see successful
code below) .aspx via the browser (?)
The error notes the applicant.xml file.... should I have that as
another xml file on my site in addition to menu.xml?

Again this is the sample code from the website you sent:

<%@ Page language="vb"%>
<%@ Import Namespace="System.XML" %>
<html><head>
<script language="VB" runat="server" ID=Script1>
Sub btnReadXML_OnClick(sender As Object, e As EventArgs)
'Read and display existing file
ReadXML(Server.MapPath("menu.xml"))
End Sub
Sub btnWriteXML_OnClick(sender As Object, e As EventArgs)
Try
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("applicant.xml"),
enc)
objXMLTW.WriteStartDocument
'Top level (Parent element)
objXMLTW.WriteStartElement("Applicant")
'Child elements, from request form
objXMLTW.WriteStartElement("Name")
objXMLTW.WriteString(Request("txtName"))
objXMLTW.WriteEndElement
objXMLTW.WriteStartElement("Address")
objXMLTW.WriteString(Request("txtAddress"))
objXMLTW.WriteEndElement
objXMLTW.WriteStartElement("City")
objXMLTW.WriteString(Request("txtCity"))
objXMLTW.WriteEndElement
objXMLTW.WriteStartElement("State")
objXMLTW.WriteString(Request("txtState"))
objXMLTW.WriteEndElement
objXMLTW.WriteStartElement("Zip")
objXMLTW.WriteString(Request("txtZip"))
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"))
Catch Ex as Exception
lblXMLFile.Text = "The following error occurred: " & Ex.Message
End Try
End Sub
Sub ReadXML(FileName as String)
Try
lblXMLFile.Text =""
Dim objXMLTR as new XMLTextReader(FileName)
dim sCategory as String
dim bNested as Boolean
dim sLastElement as String
Dim sValue as String
'Read method loops through the XML stream
Do While objXMLTR.Read
'Output elements and values
'Look at output in browser and compare to menu.xml file to
'see exactly what is being done
If objXMLTR.NodeType = XMLNodeType.Element Then
if bNested = True then
if sCategory <> "" then sCategory = sCategory & " > "
sCategory = sCategory & sLastElement
End if
bNested = True
sLastElement = objXMLTR.Name
Else If objXMLTR.NodeType = XMLNodeType.Text or _
objXMLTR.NodeType = XMLNodeType.CData Then
bNested = False
sCategory = "<P>" & sCategory
sValue = objXMLTR.value
lblXMLFile.Text = lblXMLFile.Text & "<B>" & sCategory & _
"<BR>" & sLastElement & "</B><BR>" & sValue
sLastElement = ""
sCategory = ""
End if
Loop
objXMLTR.close
Catch Ex as Exception
lblXMLFile.Text = "The following error occurred: " & Ex.Message
End Try
End Sub
</script>
</head>
<body>
<center><b>XML Text Reader/Text Writer Demo</b></center>
<form method="post" action="XML_DEMO.aspx" runat="server" ID=Form1>
<table WIDTH = "100%">
<tr>
<TD width="50%" valign = top>
Click below to read/parse the file "menu.xml".<p>
<asp:Button id="btnReadXML" text="Read XML Document"
OnClick="btnReadXML_onClick" runat="server" /><p>
<asp:label id="lblXMLFile" runat="server" /></p>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="btnWriteXML_OnClick" />
</TD>
<TD width="50%" valign = top>
Complete the fields below to create and display the XML file
"applicant.xml" (write permission for the Internet Anonymous user must
be enabled)<P>
<asp:Button id="btnWriteXML" text="Write XML Document"
OnClick="btnWriteXML_onClick" runat="server" /><p><strong>Applicant</
strong>
<p><table><tr><td>
Name:</td>
<td><asp:Textbox id=txtName runat="server" width="200"
visible="True"></asp:Textbox></td></tr>
<tr><td>Address: </td>
<td>
<asp:Textbox id="txtAddress" runat="server" visible="True"
width="200"/></td></tr>
<tr><td>
City: </td>
<td>
<asp:Textbox id=txtCity runat="server" width="200"
visible="True"></asp:Textbox></td></tr>
<tr><td>
State: </td>
<td>
<asp:Textbox id=txtState runat="server" width="200" visible="True"
MaxLength="2"></asp:Textbox></td></tr>
<tr><td>
Zip: </td>
<td>
<asp:Textbox id=txtZip runat="server" width="200"
visible="True" maxlength="10"></asp:Textbox></td></tr>
</table></p>
</TD>
</tr>
</table>
</form>
</body></html>
 
G

Guest

How can I check the security of the XML file or change it or move it?
I can view it in a browser and can display it in the (see successful
code below) .aspx via the browser (?)
The error notes the applicant.xml file.... should I have that as
another xml file on my site in addition to menu.xml?

Again this is the sample code from the website you sent:

It's a matter of access rights, not a code.

"Access to the path 'E:\kunden\homepages\26\d190091667\applicant.xml'
is denied" means that ASP.NET cannot change the file located in 'E:
\kunden\homepages\26\d190091667\'. Often it's a matter of permissions
for a directory, so I think that you have no write access in the root
directory of your web site. If you have an access to the directory
properties on your server (for example with a web site management
console), then check it. If you don't - check with your web hosting
provider for questions about where you can have such access.

You can open the xml-file in the browser, or read it using a code,
because you don't need a write access for that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top