homepage Welcome to WebmasterWorld Guest from 54.145.172.149
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
Forum Library, Charter, Moderators: ocean10000

Microsoft IIS Web Server and ASP.NET Forum

    
Editing xml file on remote server with web service
Needt to edit delete insert update xml remotely
kiwibrit

10+ Year Member



 
Msg#: 3991843 posted 7:23 pm on Sep 18, 2009 (gmt 0)

I'm new to .net, working in vb in a 3.5 environment and have come up against a brick wall.

I am working up to being able to interrogate another server for xml data, and then respond. To simulate that I am working between two web sites. The idea is to call up an xml file located at [url]http://www.example.com/WebServices/Tester/test.xml[/url] with a button click to display the content,each column headed by the appropriate node's name, then be able to insert, delete, edit and update.

At the remote site, I have created access via a web service with a file called transxml.amx.

That has the code:
<%@ WebService Language="VB" CodeBehind="~/App_Code/transxml.vb" Class="transxml" Debug="true" %>

Its code behind is:


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://example.com/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class transxml
Inherits System.Web.Services.WebService

<WebMethod()> Public Function GetXmlFile() As XmlNode
Dim doc As New XmlDocument()
doc.Load(Server.MapPath("Tester/test.xml"))
Return doc.DocumentElement
End Function
End Class

I have made a start by creating a file on the calling server - /Testxml/Books.aspx which, if you click the button, brings up the desired grid - except there is no editing facility.

Here's the code of Books.aspx:


<%@ Page Language="VB" Debug="true" AutoEventWireup="false" CodeFile="Books.aspx.vb" Inherits="Books" validateRequest="false" %>

<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Editable xml</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

<asp:Button ID="Button1" runat="server" Text="Get books" OnClick="Button1_Click" />

</div>
</form>
</body>
</html>

Here's the code behind:


Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.XmlLinkedNode
Imports System.Xml.XmlElement
Imports System.Data

Partial Class Books
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ws As New BooksInfo.transxml()
Dim element As XmlElement = DirectCast(ws.GetXmlFile(), XmlElement)
Dim ds As New DataSet()
Dim xnr As New XmlNodeReader(element)
ds.ReadXml(xnr)

GridView1.DataSource = ds
GridView1.DataBind()
End Sub

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

End Sub
End Class

But I have got severely stuck trying to do the editing bit. With no data bound directly to the grid in Books.aspx, I

did not get the usual options for editing when I clicked the grid's smart tag in visual web developer. After much experimentation, I got something that, in design view, looked like it could deliver:


<%@ Page Language="VB" Debug="true" AutoEventWireup="false" CodeFile="EditBooks.aspx.vb" Inherits="EditBooks" validateRequest="false" %>

<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Editable xml</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" DataSourceID="ObjectDataSource1"
CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoGenerateColumns="False" >
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"
SortExpression="Name" />
<asp:BoundField DataField="InnerXml" HeaderText="InnerXml"
SortExpression="InnerXml" />
<asp:BoundField DataField="InnerText" HeaderText="InnerText"
SortExpression="InnerText" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="System.IAsyncResult" DeleteMethod="EndGetXmlFile"
InsertMethod="EndGetXmlFile" SelectMethod="GetXmlFile"
TypeName="BooksInfo.transxml" UpdateMethod="EndGetXmlFile">
</asp:ObjectDataSource>

<asp:Button ID="Button1" runat="server" Text="Get books" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>

with this code behind;


Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.XmlLinkedNode
Imports System.Xml.XmlElement
Imports System.Data

Partial Class EditBooks
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ws As New BooksInfo.transxml()
Dim element As XmlElement = DirectCast(ws.GetXmlFile(), XmlElement)
GridView1.DataBind()

End Sub

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

End Sub
End Class

But I wound up with a render that I did not want - not the headings I wanted, nor the content displayed as required (though I liked the grid coming up without pushing the button) - and crashes when I try updating.

Can you help me get a grid that's like my original - but editable?

 

Ocean10000

WebmasterWorld Administrator 10+ Year Member



 
Msg#: 3991843 posted 12:37 am on Sep 19, 2009 (gmt 0)

You might want to look into using a Dataset. Since its main purpose is to act as a disconnected data source, which can be passed via webservices. It can be updated in one place (website 2) and the changes sent back to be synced (website 1). I think you will find plenty of code around showing you how to edit and update via dataset.

As for the ObjectData Source. I would not use it in this case at all. I would just bind the Dataset on initial page view (aka not a postback). And have all the databinding pointing to the dataset. And when you are ready to Save you will just updated the dataset, and send it back to the original webservice.

For Reference
Dataset [msdn.microsoft.com]

ObjectDataSource Class [msdn.microsoft.com]

kiwibrit

10+ Year Member



 
Msg#: 3991843 posted 8:00 am on Sep 19, 2009 (gmt 0)

Aha. I think I begin to see a glimmer of light on this - I shall work along those lines. Thanks.

kiwibrit

10+ Year Member



 
Msg#: 3991843 posted 7:13 am on Sep 21, 2009 (gmt 0)

I have found quite a lot of explanations and examples based on C# , but for the moment I am working only in VB untile I feel I have a reasonable grasp of .net; as yet I have not learned any C#. As I get more into asp.net, I am beginning to think I chose the wrong language to start with, since so many examples on the net are in C# rather than VB. More than that, an awful lot of examples relating to grid view show enabling of updating via the smart tag - but in the only way I have been able to bring in a data set for binding data to the gridview, the options are not offered.

There has been some progress:


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="author" HeaderText="Author" />
<asp:BoundField DataField="title" HeaderText="Title" />
<asp:BoundField DataField="content" HeaderText="Content" />
</Columns>
</asp:GridView>

and

Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
Dim ws As New BooksInfo.transxml()
Dim element As XmlElement = DirectCast(ws.GetXmlFile(), XmlElement)
Dim ds As New DataSet()
Dim xnr As New XmlNodeReader(element)
ds.ReadXml(xnr)
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
End Class

shows the the data as I want, and allows me to edit the fields - but I have been unable to work out how to update - and I have yet to move on to cancelling and deleting.

If you could point me at a reference using VB and a dataset for a similar task - or show some sample code, I would be very grateful.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Microsoft / Microsoft IIS Web Server and ASP.NET
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved