
|

|
:: Web Services :: XWebForum :: Implementation Guides ::
|
Build Your Own XML/SOAP Web Service Based Online Forum: Display A Forum Topic
|
|
The procedures in this implementation guide require that you have some knowledge of XML and
ASP.NET web development with the Microsoft Visual Basic.NET development tool.
The steps in this "how-to" implementation guide are:
To download the source code for this "how-to" implementation guide, as well as for additional
hints and tips, please read our Final Notes.
|
 |
Add a "Web Reference" to the XWebForum Web Service
-
In your web project, add a Web Reference to the XWebForum web service. Here are 2 ways you can
accomplish this:
- Browse the Solution Explorer, right click on References and select
Add Web Reference... from the drop down menu.
- From the File Menu, select Project - Add Web Reference....
The Add Web Reference pop-up window will open. In the URL textbox, enter the following URI:
http://ws.xwebservices.com/XWebForum/XWebForum.asmx?wsdl
- Hit the Enter key or click the Go button located to the right of the URL textbox.
- Change the Web reference name to XWebForum for easier reference.
- Click the Add Reference button to complete this step.
|
 |
Add a "Web Form" to the Web Application
|
 |
Write code in the "Codebehind" of the web form to retrieve the Forum Topic record by consuming the XWebForum Web Service
Based on the documentation for the XWebForum web service, in order to successfully consume the web service, we will
need to authenticate first using SOAP Headers. Here's how:
- Open the "Codebehind" of the topic.aspx web form we added to the web project in the
previous step.
Since the web service requires SOAP Headers for authentication purposes, we must first create a new
instance of the class representing the SOAP Header and then populate the SOAP Header values:
Dim objAuth As New XWebForum.AuthHeader
objAuth.LoginName = "<my_login_name>"
objAuth.Password = "<my_password>"
NOTE***: Instead of hard-coding the LoginName and Password values, you may want to
dynamically read them from the web.config file (LOGIN_NAME and PASSWORD must be added to the
<AppSettings>
section of the web.config file):
objAuth.LoginName = System.Configuration.ConfigurationSettings.AppSettings("LOGIN_NAME")
objAuth.Password = System.Configuration.ConfigurationSettings.AppSettings("PASSWORD")
Next, we need to create a new instance of the proxy class and assign the SOAP Header object to the member
variable of the proxy class representing the SOAP Header:
Dim objForum As New XWebForum.XWebForum
objForum.AuthHeaderValue = objAuth
We are now ready to call the method on the proxy class that communicates with the Web Service method and returns the
Forum Topic record we want displayed on the web page.
Dim strTopicXML As String = objForum.LoadTopic(Request.QueryString("tid"))
|
 |
Implement ASP.NET Labels to display the Forum Topic's details
In the Codebehind:
Declare the 4 labels which will display the News Article's details (Subject, Author, Body
and Started Date):
Protected lblSubject, lblAuthor, lblBody, lblDateStarted As System.Web.UI.WebControls.Label
Load the XML string we previously stored in the strTopicXML variable into an
XMLDocument:
Dim objDOM As New System.Xml.XmlDocument
objDOM.LoadXml(strTopicXML)
Set the labels' Text property to the corresponding Element or Attribute value:
lblSubject.Text = objDOM.DocumentElement.SelectSingleNode("Subject").InnerText
lblBody.Text = objDOM.DocumentElement.SelectSingleNode("Body").InnerText
lblAuthor.Text = objDOM.DocumentElement.SelectSingleNode("Author").InnerText
lblDateStarted.Text = objDOM.DocumentElement.Attributes.ItemOf("Date_Created").Value
In the HTML Editor, we simply define the 4 ASP.NET labels which will display the Forum Topic's details. Here is the code:
<asp:Label ID="lblSubject" Runat="server" />
<asp:Label ID="lblAuthor" Runat="server" />
<asp:Label ID="lblDateStarted" Runat="server" />
<asp:Label ID="lblBody" Runat="server" />
|
 |
Implement a Repeater to display the list of Forum Topic's Replies
In the Codebehind, we need to declare a Repeater and then bind the Replies node to the Repeater's DataSource. Here's how:
Declare the Repeater in the Codebehind:
Protected rptReplies As System.Web.UI.WebControls.Repeater
Check if the Replies node has child nodes and if so, bind the data to the repeater
If objDOM.DocumentElement.SelectSingleNode("Replies").ChildNodes.Count
> 0 Then
rptReplies.DataSource =
objDOM.DocumentElement.SelectSingleNode("Replies")
rptForum.rptReplies()
End If
In the HTML Editor, we format the Repeater to our liking to display the list of Forum Replies. We will display the
Subject, Body, Author and the date the reply was created (Date_Created attribute's value).
Here are the necessary steps:
Add the Repeater inside the body of your HTML page:
<asp:Repeater ID="rptReplies" Runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
Inside the Repeater's ItemTemplate, first we display the Subject of the Reply. Since the Repeater is looping through XML Nodes
(<Message>), we need to
bind to the <Subject> element, then obtain their value:
<b>
<%# Container.DataItem.SelectSingleNode("Subject").InnerText %>
</b>
Next, we display the Body of the Reply. Again, since the Repeater is looping through XML Nodes
(<Message>),
we need to bind to the
<Body> element,
then get its value:
<%# Container.DataItem.SelectSingleNode("Body").InnerText %>
Finally, we display the Forum Topic's author and created date. Since
Date_Created is an attribute, we must handle it slightly different than an element:
<%# Container.DataItem.SelectSingleNode("Author").InnerText %>
<%# Container.DataItem.Attributes.ItemOf("Date_Created").Value %>
|
 |
Final Notes
The source code for this "how-to" implementation guide can be downloaded by clicking the link
below (zip file, contains the source code for all "how-to" implementation guides for the
XWebForum Web Service)
XWebForumClient.zip
It's always good practice to implement a Try...Catch... error handling technique to handle some or all
possible errors that may occur while the code is executing. We strongly recommend that you implement such
a technique.
|
|

|

|
|