edit shared XML via asp.net page and save back to server.

G

Glenn M

I have a shared XML file on a server .

i also have one xslt file that performs a simple transform on in to
view the data.

now i want to have another page that lets users modify the shared xml
file via some editable controls such as text boxes , option boxes etc.

how can i implment this , should i use another xslt file with <INPUT>
controls . if so how can i save the result back using the asp.net
page.

or should i use asp.net to drive the whole page and not use an xslt
file . again , any pointers on how to do this would be useful.

thanks

glenn
 
G

Guest

I would stick with the ASP.NET approach. The DataGrid/DataSet has native XML support. You could populate the DataGrid with the XML file, allow users to edit the information inside of the DataGrid, and then save the XML file. OR -> You could create a custom object(s) that represent the XML file and serialize and deserialize the XML as necessary. In that form, you could continue to use text boxes, drop down lists, data grids, etc.
 
J

Jared

First, there is no reason to reinvent the wheel, if you are alredy using an
xslt transform and it does everything you need it to do, leave it alone. On
your edit page you could use an xmldocument object to write/save the data in
the xml document. When the user displays the "View" page the xlst will
reload the xml file and display the edited/updated information. Make sure
the authentication method you have chosen has write access to the xml file,
I used "everyone - write".

Here is a working sample
There are six files in this example, two webforms (View.aspx, Edit.aspx),
two code behinds, an xml document and an xlst file. Set View.aspx as the
startup page.

Hope this helps.

'View.aspx
<HTML>
<HEAD>
<title>Default</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
&nbsp;
<asp:button id="btnEdit" style="Z-INDEX: 101; LEFT: 12px; POSITION:
absolute; TOP: 13px" runat="server"
Text="Edit" Width="102px"></asp:button></form>
</body>
</HTML>

' End View.aspx

'View.aspx.vb
Public Class View
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnEdit As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim doc As New System.Web.UI.WebControls.Xml
Me.Controls.Add(doc)
doc.DocumentSource = "Books.xml"
doc.TransformSource = "Books.xslt"
End If
End Sub

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEdit.Click
Response.Redirect("edit.aspx")
End Sub
End Class

' End View.aspx.vb

'Edit.aspx
<HTML>
<HEAD>
<title>Edit</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div>
<asp:DropDownList id="lstBooks" runat="server" Width="189px"
AutoPostBack="True"></asp:DropDownList>
<asp:Button id="btnSave" runat="server" Width="131px"
Text="Save"></asp:Button>
<asp:Button id="btnDone" runat="server" Width="131px"
Text="Finished"></asp:Button>
</div>
<div>
<table runat="server">
<tr width="100%">
<td width="30%">Author</td>
<td runat="server">
<asp:TextBox id="txtAuthor" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Title</td>
<td>
<asp:TextBox id="txtTitle" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Genre</td>
<td>
<asp:TextBox id="txtGenre" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Price</td>
<td>
<asp:TextBox id="txtPrice" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Date Published</td>
<td>
<asp:TextBox id="txtPubDate" runat="server"
Width="100%"></asp:TextBox></td>
</tr>
<tr width="100%">
<td width="30%">Description</td>
<td>
<asp:TextBox id="txtDescription" runat="server" Width="100%"
TextMode="MultiLine"></asp:TextBox></td>
</tr>
</table>
</div>
</form>
</body>
</HTML>

'End Edit.aspx

'Edit.aspx.vb
Public Class Edit
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents btnSave As System.Web.UI.WebControls.Button
Protected WithEvents lstBooks As System.Web.UI.WebControls.DropDownList
Protected WithEvents txtAuthor As System.Web.UI.WebControls.TextBox
Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
Protected WithEvents txtGenre As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPrice As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPubDate As System.Web.UI.WebControls.TextBox
Protected WithEvents txtDescription As System.Web.UI.WebControls.TextBox
Protected WithEvents btnDone As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Dim xDoc As System.Xml.XmlDocument

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack _
AndAlso (Session.Item("IsDirty") = True _
OrElse Session.Item("IsDirty") Is Nothing) Then
xDoc = New System.Xml.XmlDocument
xDoc.Load(Server.MapPath("Books.xml"))
Dim Nodes As System.xml.XmlNodeList = xDoc.SelectNodes("//book")
For Each Node As System.Xml.XmlNode In Nodes
Me.lstBooks.Items.Add(Node.Item("title").InnerText)
Next
LoadData()
Session.Item("IsDirty") = False
End If

End Sub

Private Sub LoadData()
Me.txtAuthor.Text = GetElement("author")
Me.txtDescription.Text = GetElement("description")
Me.txtGenre.Text = GetElement("genre")
Me.txtPrice.Text = GetElement("price")
Me.txtPubDate.Text = GetElement("publish_date")
Me.txtTitle.Text = GetElement("title")
End Sub
Private Function GetElement(ByVal TagName As String) As String
If xDoc Is Nothing Then
xDoc = New System.Xml.XmlDocument
xDoc.Load(Server.MapPath("Books.xml"))
End If
Dim Path As String = "//book[title=""" & Me.lstBooks.SelectedValue &
"""]"
Dim Node As System.Xml.XmlNode = xDoc.SelectSingleNode(Path)
Return Node.Item(TagName).InnerText
End Function

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
If xDoc Is Nothing Then
xDoc = New System.Xml.XmlDocument
xDoc.Load(Server.MapPath("Books.xml"))
End If
Dim Path As String = "//book[title=""" & Me.lstBooks.SelectedValue &
"""]"
Dim node As System.Xml.XmlNode = xDoc.SelectSingleNode(Path)
With node
.Item("title").InnerText = Me.txtTitle.Text
.Item("author").InnerText = Me.txtAuthor.Text
.Item("description").InnerText = Me.txtDescription.Text
.Item("publish_date").InnerText = Me.txtPubDate.Text
.Item("price").InnerText = Me.txtPrice.Text
.Item("genre").InnerText = Me.txtGenre.Text
End With
xDoc.Save(Server.MapPath("books.xml"))
Session.Item("IsDirty") = True
End Sub

Private Sub lstBooks_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles lstBooks.SelectedIndexChanged
LoadData()
End Sub

Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDone.Click
Response.Redirect("view.aspx")
End Sub
End Class

'End Edit.aspx.vb

'Books.xslt
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="catalog">
<HTML>
<BODY>
<TABLE BORDER="2">
<TR>
<TD>Title</TD>
<TD>Author</TD>
<TD>Genre</TD>
<TD>Price</TD>
<TD>Date Published</TD>
<TD>Description</TD>
</TR>
<xsl:for-each select="book">
<TR>
<TD><xsl:value-of select="title"/></TD>
<TD><xsl:value-of select="author"/></TD>
<TD><xsl:value-of select="genre"/></TD>
<TD><xsl:value-of select="price" /></TD>
<TD><xsl:value-of select="publish_date" /></TD>
<TD><xsl:value-of select="description" /></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

'End Books.xslt

'Books.xml
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk105">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk106">
<author>Randall, Cynthia</author>
<title>Lover Birds</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
<description>When Carla meets Paul at an ornithology
conference, tempers fly as feathers get ruffled.</description>
</book>
<book id="bk107">
<author>Thurman, Paula</author>
<title>Splish Splash</title>
<genre>Romance</genre>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
<description>A deep sea diver finds true love twenty
thousand leagues beneath the sea.</description>
</book>
<book id="bk108">
<author>Knorr, Stefan</author>
<title>Creepy Crawlies</title>
<genre>Horror</genre>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
<description>An anthology of horror stories about roaches,
centipedes, scorpions and other insects.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg
Uncertainty Device, James Salway discovers the problems
of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in
detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<author>O'Brien, Tim</author>
<title>MSXML3: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
<description>The Microsoft MSXML3 parser is covered in
detail, with attention to XML DOM interfaces, XSLT processing,
SAX and more.</description>
</book>
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>

'End Books.xml
 
G

Glenn Mantle

Thanks for that.

I have another question , i have a vbscript on server1 that parses an
xml file from my web server sever2 (http:\\server2\myxml.xml)

The vbscript collects information from the server and amends the xml dom
object when a new record needs to be added or old one removed.

Now i want want to persist that xml back to the web server , and as we
know i can't save back directly. Any idea how i can work around this.

I was thinking of setting up a web sevice on the server or maybe feeding
the xml stream stream into an ASP.net page that would save it to the xml
file.

(alas the vbscript must reside on server1 and the xml file on sever2 )

any starting points to look at would be really useful.

glenn
 
J

Jared

Glenn,
Why can't you save the back to the web server? Do you mean it is
physically impossible; or you don't want to make security permission changes
to the virtual directory/file that needs to be updated? If you give write
access to the account the script runs against you shouldn't have a problem
modifying the script as long as you aren't trying to delete the file and
create a new one each time. You can use a web service as you suggested,
but, you will still have to account for security permissions which would
essentially do the same as modifying the DACL on the file itself. Is you
web server available to the public, or an Intranet server? Does your site
run under the IUSER account, Basic, or Windows auth? Can \\server1 access
\\server2 vice versa ? I have written web applications that will write xml
files to a different physical location, the only obstacle I have come across
is physical file security.

I would first try to modify the permissions on
\\server2\wwwroot$\your_virtual_directory\xmlfile.xml, make sure the user
account running the script can write to it. Modify your script to make the
necessary changes in the xml file via the XMLDOM object and you should be
good to go. If you want to replace \\server2\myxml.xml with the newly
generated copy from \\server1 then you will need to grant modify permissions
to the virtual directory where the file is stored and modify your script to
include:

set FSO = CreateObject("Scripting.FileSystemObject")
Set MyXMLFile = FSO.GetFile("C:\MyXML.xml")
MyXMLFile.Copy ("\\Server2\wwwroot$\your_virtual_directory\MyXML.xml", True)
'True = Overwrite

Post your results, or any other details.
Jared
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top