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"))
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>
:-)
What errors do you get?
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...
There are tons of articles and examples about that subject on the web
For example: http://www.freevbcode.com/ShowCode.asp?ID=2789
<%@ 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>
__________________________________________________________________
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
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" />
Brian, it contans two files: menu.xml and XML_DEMO.aspx
XML_DEMO.aspx has an example of the code
'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
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>
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.