AWP Unit 5
AWP Unit 5
Topics:
XML| Security Fundamentals |ASP.NET AJAX
Q1. What is XML? List the various XML classes.
Extensible Markup Language (XML) stores and transports data. If we use a XML file to store the data
then we can do operations with the XML file directly without using the database. The XML format is
supported for all applications.
It is independent of all software applications and it is accessible by all applications. It is a very widely
used format for exchanging data, mainly because it's easy readable for both humans and machines. If
we have ever written a website in HTML, XML will look very familiar to us, as it's basically a stricter
version of HTML. XML is made up of tags, attributes and values and looks something like this:
<?xmlversion="1.0"encoding="utf-8"?>
<EmployeeInformation>
<Details>
<Name>Richa</Name>
<Emp_id>1</Emp_id>
<Qualification>MCA</Qualification>
</Details>
</EmployeeInformation>
XML Classes:
ASP.NET provides a rich set of classes for XML manipulation in several namespaces that start with
System.Xml. The classes here allow us to read and write XML files, manipulate XML data in memory,
and even validate XML documents.
The following options for dealing with XML data:
XmlTextWriter
The XmlTextWriter class allows us to write XML to a file. This class contains a number of methods and
properties that will do a lot of the work for us. To use this class, we create a new XmlTextWriter object.
XmlTextReader
Reading the XML document in our code is just as easy with the corresponding XmlTextReader class.
The XmlTextReader moves through our document from top to bottom, one node at a time. We call the
Read() method to move to the next node. This method returns true if there are more nodes to read or
false once it has read the final node.
XDocument
The XDocument class contains the information necessary for a valid XML document. This includes an
XML declaration, processing instructions, and comments. The XDocument makes it easy to read and
navigate XML content. We can use the static XDocument.Load() method to read XML documents from
a file, URI, or stream.
Q2. What is XmlTextWriter? Explain with example
XmlTextWriter is one of many useful classes in System.Xml namespace, and it provides several useful
methods to write a complete XML Document from scratch.
Following form is simple ASP.NET web form asking user to input some data about the product. I will
save this data in a separate XML document named Product.xml using XmlTextWriter object.
93
Following C# code is saving all the user input values in XML file.
protected void Button1_Click(object sender, EventArgs e)
{
XmlTextWriter writer = null;
try
{
string filePath = Server.MapPath("~") + "\\Product.xml";
writer = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteComment("Created On: " + DateTime.Now.ToString("dd-MMM-yyyy"));
writer.WriteComment("===============================");
writer.WriteStartElement("Product");
writer.WriteAttributeString("ProductID", TextBox1.Text);
writer.WriteElementString("ProductName", TextBox2.Text);
writer.WriteElementString("ProductQuantity", TextBox3.Text);
writer.WriteElementString("ProductPrice", TextBox4.Text);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
Response.Redirect("Product.xml");
}
catch (Exception ex){ }
}
Q3. What is XmlTextReader? Explain with example
Reading the XML document in our code is just as easy with the corresponding XmlTextReader class.
The XmlTextReader moves through our document from top to bottom, one node at a time.
Example:
TernTenders.xml
<?xml version="1.0" encoding="utf-8" ?>
<Tenders>
<Ravina>
<BillNo>1</BillNo>
<PageNo>10</PageNo>
<Activity>Metals</Activity>
</Ravina>
<Ravina>
<BillNo>2</BillNo>
<PageNo>20</PageNo>
<Activity>Formworks</Activity>
</Ravina>
< Ravina>
<BillNo>3</BillNo>
<PageNo>30</PageNo>
<Activity>SiteWorks</Activity>
</ Ravina>
</Tenders>
ASPX Page:
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
<asp:DropDownList ID="DropDownList1BillNo" runat="server"></asp:DropDownList>
94
<asp:DropDownList ID="DropDownList2PageNo" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList3Activity" runat="server"></asp:DropDownList>
Code behind Page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataToGridview();
}
}
protected void BindDataToGridview()
{
XmlTextReader xmlreader = new
XmlTextReader(Server.MapPath("~/App_Data/TernTenders.xml"));
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count != 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
DropDownList1BillNo.DataSource = ds;
DropDownList1BillNo.DataTextField = "BillNo";
DropDownList1BillNo.DataValueField = "BillNo";
DropDownList1BillNo.DataBind();
DropDownList2PageNo.DataSource = ds;
DropDownList2PageNo.DataTextField = "PageNo";
DropDownList2PageNo.DataValueField = "PageNo";
DropDownList2PageNo.DataBind();
DropDownList3Activity.DataSource = ds;
DropDownList3Activity.DataTextField = "Activity";
DropDownList3Activity.DataValueField = "Activity";
DropDownList3Activity.DataBind();
}
}
95
Example:
TernTenders.xml
<?xml version="1.0" encoding="utf-8" ?>
<Tenders>
<Ravina>
<BillNo>1</BillNo>
<PageNo>10</PageNo>
<Activity>Metals</Activity>
</Ravina>
<Ravina>
<BillNo>2</BillNo>
<PageNo>20</PageNo>
<Activity>Formworks</Activity>
</Ravina>
< Ravina>
<BillNo>3</BillNo>
<PageNo>30</PageNo>
<Activity>SiteWorks</Activity>
</ Ravina>
</Tenders>
ASPX Page:
<asp:Button ID="ReadXmlUsingXMLDocument" runat="server"
Text="ReadXMLDataUsingXMLDocument" OnClick="ReadXmlUsingXMLDocument_Click" />
<asp:ListBox ID="ListBox2" runat="server" Height="500px" Width="500px"></asp:ListBox>
Code behind Page
protected void ReadXmlUsingXMLDocument_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/App_Data/TernTenders.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("Activity");
for (int i = 0; i < elemList.Count; i++)
{
ListBox2.Items.Add(elemList[i].InnerXml);
}
}
96
<?xml version="1.0" encoding="utf-8" ?>
<Authors>
<Author Name="Mahesh Chand">
<Book>GDI+ Programming</Book>
<Cost>$49.95</Cost>
<Publisher>Addison-Wesley</Publisher>
</Author>
<Author Name="Mike Gold">
<Book>Programmer's Guide to C#</Book>
<Cost>$44.95</Cost>
<Publisher>Microgold Publishing</Publisher>
</Author>
<Author Name="Scott Lysle">
<Book>Custom Controls</Book>
<Cost>$39.95</Cost>
<Publisher>C# Corner</Publisher>
</Author>
</Authors>
Code Behind
XElement allData = XElement.Load(Server.MapPath("~/App_Data/ Authors.xml")
if (allData != null)
{
IEnumerable<XElement> authors = allData.Descendants("Author");
foreach(XElement author in authors)
Response.Write ((string) author);
}
Q6. What is XML Validation?
The validation of an Xml input File could occur at various instances of processing as mentioned below:
using a schema file
In the Database Code for validating against business rules.
All Xml code can be considered as categorically correct if they are well-formed and valid xml files.
Well-formed - The XML code must be syntactically correct or the XML parser will raise an error.
Valid - If the XML file has an associated XML Schema, the elements must appear in the defined
structure and the content of the individual elements must conform to the declared data types
specified in the schema.
Validation of Xml Files can be achieved through the use of various Technologies ex. Visual Studio Net.
Also, there are many on-line Tools available for validating an input .xml File. Few of them are
mentioned below:
Xml Schema Validation
An Xml File is generally validated for its conformance to a particular schema. The Xml schema file
usually is a XML Schema definition language (XSD). The input Xml File could be even validated against a
set of Schema Files. The Schema File is the structural representation of how a given Xml Data File
should resemble.
Validation using XSL Technology:
XSLT [Extended Stylesheet Language Transformation] is basically used for transforming the input xml
File from one form to another, which could be .html, .xml etc. Here, we use XSLT to transform the
input Xml File to a form that we require for further processing
97
Q7. What is XML Transform using XslCompiledTransform Class? Explain with example
In the .NET Framework a class XslCompiledTransform is present in the System.Xml.Xsl namespace that
can be used to do the transformation. Then theXslCompiledTransform object calls the Load () method
to load the XSLT file content into the object.
Example:
Data.xml
<?xml version='1.0'?>
<bookstore>
<book genre="A" publicationdate="1981" ISBN="1-11111-11-0">
<title>title 1</title>
<author>
<first-name>A</first-name>
<last-name>B</last-name>
</author>
<price>8</price>
</book>
<book genre="B" publicationdate="1999" ISBN="0-222-22222-2">
<title>title 2</title>
<author>
<first-name>C</first-name>
<last-name>D</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
Data.xsl
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:element name="Authors">
<xsl:apply-templates select="//book"/>
</xsl:element>
</xsl:template>
<xsl:template match="book">
<xsl:element name="Author">
<xsl:value-of select="author/first-name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="author/last-name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
98
xslt.Load(xsltFile);
Windows-based authentication:
It causes the browser to display a login dialog box when the user attempts to access restricted page.
It is supported by most browsers.
It is configured through the IIS management console.
It uses windows user accounts and directory rights to grant access to restricted pages.
Forms-based authentication:
Developer codes a login form that gets the user name and password.
The username and password entered by user are encrypted if the login page uses a secure
connection.
It doesn’t reply on windows user account.
100
To use the WAT for this type of configuration, select Website ➤ ASP.NET Configuration from the menu.
Next, click the Security tab.
We will see the window shown in figure, which gives us links to set the authentication type, define
authorization rules (using the Access Rules section), and enable role-based security. (Role-based security
is an optional higher-level feature we can use with forms authentication.
101
AJAX uses several existing technologies together, including: XHTML, CSS, JavaScript, Document Object
Model, XML, XSLT, and the XMLHttpRequest object.
With AJAX, web applications can retrieve data from the server asynchronously, in the background,
without reloading the entire browser page. The use of AJAX has led to an increase in interactive
animation on web pages
Advantages
Reduces the traffic travels between the client and the server.
No cross browser pains.
Better interactivity and responsiveness.
With AJAX, several multipurpose applications and features can be handled using a single web
page(SPA).
API's are good because those work with HTTP method and JavaScript.
Disadvantages
Search engines like Google would not be able to index an AJAX application.
It is totally built-in JavaScript code. If any user disables JS in the browser, it won't work.
The server information cannot be accessed within AJAX.
Security is less in AJAX applications as all the files are downloaded at client side.
The data of all requests is URL-encoded, which increases the size of the request.
Q13. What are the benefits using Ajax? Explain UpdatePanel and ScriptManager.
The major benefit of Ajax is partial page rendering. The partial update of a page does not necessitate
full reload of the page and hence leads to flicker-free page rendering.
UpdatePanel
We can refresh the selected part of the web page by using UpdatePanel control, Ajax UpdatePanel
control contains a two child tags that is ContentTemplate and Triggers. In a ContentTemplate tag we
used to place the user controls and the Trigger tag allows us to define certain triggers which will
make the panel update its content.
<asp:UpdatePanel ID="updatepnl" runat="server">
<ContentTemplate>
All the contents that must be updated asynchronously (only ContentTemplate parts are updated
and rest of the web page part is untouched) are placed here. It allows us to send request or post
data to server without submit the whole page so that is called asynchronous.
UpdatePanel is a container control. A page can have multiple update panels.
The UpdatePanel control enables you to create a flicker free page by providing partial-page update
support to it.
It identifies a set of server controls to be updated using an asynchronous post back.
If a control within the UpdatePanel causes a post back to the server, only the content within that
UpdatePanel is refreshed.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
102
ScriptManager
The ScripManager Control manages the partial page updates for UpdatPanel controls that are on
the ASP.NET web page or inside a user control on the web page.
This control manages the client script for AJAX-enabled ASP.NET web page and ScripManager
control support the feature as partial-page rendering and web-service calls.
The ScriptManager control manages client script for AJAX-enabled ASP.NET Web pages.
Although this control is not visible at runtime, it is one of the most important controls for an Ajax
enabled web page.
There can be only one ScriptManager in an Ajax enabled web page.
103
</ContentTemplate>
</asp:UpdatePanel>
In the code shown above, we use the AssociatedUpdatePanelID property of the UpdateProgress
control to associate it with an UpdatePanel control. The ProgressTemplate property that can contain
HTML, is used to specify the message displayed by an UpdateProgress control. In our case, we are
displaying a .gif image progress as shown below.
104
ASP.Net Ajax Control Toolkit contains 40 + ready controls which is easy to use for fast productivity.
Controls are available in the Visual Studio Toolbox for easy drag and drop integration with our web
application. Some of the controls are like AutoComplete, Color Picker, Calendar, Watermark, Modal
Popup Extender, Slideshow Extender and more of the useful controls.
The ASP.Net AJAX Control toolkit is now maintained by DevExpress Team. The Current Version of
ASP.Net AJAX Toolkit is v16.1.0.0. There are lot of new enhancement in the current version from new
controls to bug fixes in all controls.
The ASP.NET AJAX Control Toolkit has a lot going for it:
It’s completely free.
It includes full source code, which is helpful if we’re ambitious enough to want to create our own
custom controls that use ASP.NET AJAX features.
It uses extenders that enhance the standard ASP.NET web controls. That way, we don’t have to
replace all the controls on our web pages.
105
4. Add AccordionPane in Panes collection of the Accordion.
AccordionPane contains two parts i.e. Header and Content. When AccordionPane is collapsed,
only Header part is visible to us.
107