XML Basics
XML Basics
NET-2
XML Basics:
• XML stands for eXtensible Markup Language.
• XML is a markup language much like HTML.
• XML was designed to store and transport data.
• XML was designed to be self-descriptive.
• XML is a W3C Recommendation.
• XML is not a replacement for HTML.
• XML is designed to carry data, not to display data.
• XML tags are not predefined. You must define your own tags.
• XML is platform independent and language independent.
• XML is a software- and hardware-independent tool for storing and transporting data.
1) Difference Between XML and HTML:
HTML is a popular language and contains more than 100 tags to display data in webpages. But
it is hard to remember and use all the tags in HTML. And also, HTML’s presentation changes
according to browsers. One more problem is that in HTML document we use more tags than
actual original content. These all problems are solved using XML by combining XML and
HTML i.e. by using X-HTML. The main benefit of xml is that you can use it to take data from
a program like Microsoft SQL, convert it into XML then share that XML with other programs
and platforms. You can communicate between two platforms which are generally very difficult.
One more important difference is XML and HTML were designed with different goals: XML
was designed to carry data - with focus on what data is and HTML was designed to display
data - with focus on how data looks.
2) Features of xml :
• XML is powerful and excellent option to handle complex structure of data.
• By using xml we can give description of data in text format.
• Extensible Markup Language (XML) is a markup language that defines a set of rules for
encoding documents in a format that is both human-readable and machine-readable.
• Xml handles the data in tree structure.
• For storing data for a long time and using that data xml is better choice than any other.
3) XML Example :
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>
<person>
<name>Ram</name>
<age>24</age>
</person>
4) .NET Support for XML :
Although many programming languages and environments have provided XML support as an
add-on, .NET’s support is integrated into the framework more tightly than most. The .NET
development team decided to use XML extensively within the framework in order to meet its
design goals. Accordingly, they built in XML support from the beginning. The .NET
Framework has extensive support for working with XML documents. In the .NET framework,
the support for XML documents includes:
XML namespace
XML designer
XML Web Server control
XML DOM support
1. XML Namespace
The System.Xml namespace provides a rich set of classes for processing XML data. The
commonly used classes for working with XML data are:
• XmlTextReader: Provides forward only access to a stream of XML data and checks
whether or not an XML document is well formed. This class neither creates as in-memory
structure nor validates the XML document against the DTD. You can declare an object of
the XmlTextReader class by including the System.Xml namespace in the application. The
syntax to declare an object of this class is as follows:
XmlTextReader reader = new XmlTextReader("XML1.xml");
• XmlTextWriter: Provides forward only way of generating streams or files containing XML
data. If you want to declare an object of the XmlTextWriter class, you must include the
System.Xml. The syntax to declare an object of this class is as follows:
XmlTextWriter writer = new XmlTextWriter(Response.Output);
• XmlDataDocument: Provides support for XML and relational data in XML DOM. You
can use this class with a dataset to provide relational and non-relational views of the same
set of data. This class is primarily used when you want to access the functions of
ADO.NET. The syntax to declare an object of this class is as follows:
DataSet ds=new DataSet();
XmlDataDocument doc=new XmlDocument(ds);
2. XML Designer
Visual Studio .NET provides the XML designer that you can use to create and edit XML
documents.
3. XML Web Server Control
An XML Web Server control is used to display the contents of an XML document without
formatting or using XSL Transformations. You can optionally specify a XSLT style sheet that
formats the XML document before it is displayed in an XML server control. The XML Web
Server control belongs to the System.Web.UI. Web Controls namespace. You can add an XML
Web Server control to a Web form by dragging the control from the Web forms tab of the
toolbox.
4. XML Document Object Model Support
When you want to access and display XML data in Web Applications, you use the XML Web
server control and set its properties at design time. In certain situation, you may need to display
the XML data based on specific conditions. In such cases, you will have to access the XML
data programmatically.
An XML document consists of elements and attributes. For this reason, you can access XML
data programmatically. Note that XML DOM allows you to access and manipulate the elements
and attributes present in an XML document programmatically.
XML Validation:
Example:
Following is an example of a well-formed XML document −
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address
[
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
External DTD : In external DTD elements are declared outside the xml file.
2. XSD :
XML schema is commonly known as XML Schema Definition (XSD).
It is used to describe and validate the structure and the content of xml data.
Xml schema defines the elements, attributes and data types.
XML schema is a XML based alternative to DTD. Actually DTD and XML schema both are
used to form a well-formed XML document.
• Types of XSD:
Example :
State Management:
State management in any application plays a vital role in preserving the states of users, web
pages, objects, or controls. As .NET developers know, HTTP is a stateless protocol, so all
online ASP.NET web applications are also stateless by default. That means the web application
cannot preserve the state of control for each page submitted to the server. In the modern era of
web applications, there is a high demand for state management. We know that web applications
work on client-server technology and follows the HTTP protocol. As HTTP protocol is a
stateless protocol, it does not store the information provided by the user. This is because the
web applications are deployed on a centralized server and the request can come from any
random client machine. If the server starts retaining each and every information provided by
the user then it will lead to the unnecessary consumption of memory and could even run out of
storage. So, Asp.net provides state management techniques to restore the data provided by the
user. State Management could also be defined as the process of the persistence of the values
between multiple request-response cycles. These state management techniques are of two
types:-
• Client-Side State Management Techniques - To maintain the state of values on the client’s
machine, we use the client-side state management techniques. Whenever we use Client-
Side State Management, the state related information will directly get stored on the client-
side. That specific information will travel back and communicate with every request
generated by the user then afterwards provides responses after server-side communication.
Client side state management techniques are :
1. Hidden field
Hidden field is a control provided by ASP.NET which is used to store small amounts of data
on the client. It stores one value for the variable and it is a preferable way when a variable's
value is changed frequently. Hidden field control is not rendered to the client (browser) and it
is invisible on the browser. A hidden field travels with every request like a standard control’s
value.
2. View state
View state is another client-side state management mechanism provided by ASP.NET to store
user's data, i.e., sometimes the user needs to preserve data temporarily after a post back, then
the view state is the preferred way for doing it. It stores data in the generated HTML using
hidden field not on the server. View State provides page level state management i.e., as long
as the user is on the current page, state is available and the user redirects to the next page and
the current page state is lost. View State can store any type of data because it is object type but
it is preferable not to store a complex type of data due to the need for serialization and
deserialization on each post back. View state is enabled by default for all server side controls
of ASP.NET with a property EnableviewState set to true.
3. Cookies
Cookie is a small text file which is created by the client's browser and also stored on the client
hard disk by the browser. It does not use server memory. Generally, a cookie is used to identify
users. A cookie is a small file that stores user information. Whenever a user makes a request
for a page the first time, the server creates a cookie and sends it to the client along with the
requested page and the client browser receives that cookie and stores it on the client machine
either permanently or temporarily (persistent or non persistence). The next time the user makes
a request for the same site, either the same or another page, the browser checks the existence
of the cookie for that site in the folder. If the cookie exists it sends a request with the same
cookie, else that request is treated as a new request.
Types of Cookies
1. Persistence Cookie: Cookies which you can set an expiry date time are called persistence
cookies. Persistence cookies are permanently stored till the time you set.
2. Non-Persistence Cookie: Non persistence cookies are not permanently stored on the user
client hard disk folder. It maintains user information as long as the user accesses the same
browser. When user closes the browser, the cookie will be discarded. Non-Persistence cookies
are useful for public computers.
4. Control State
Control State is another client side state management technique. Whenever we develop a
custom control and want to preserve some information, we can use view state but suppose view
state is disabled explicitly by the user, the control will not work as expected. For expected
results for the control we have to use Control State property. Control state is separate from
view state.
• Server-Side State Management Techniques - To maintain the state of values on the server’s
machine, we use the server-side state management techniques. Server-Side State
Management is different from Client-Side State Management but the operations and
working is somewhat the same in functionality. In Server-Side State Management all the
information is stored in the user memory. Due to this functionality, there are more secure
domains at the server side in comparison to Client-Side State Management. Server side
state management techniques are :
1. Session state:
Session management is a very strong technique to maintain state. Generally, session is used to
store user's information and/or uniquely identify a user (or say browser). The server maintains
the state of user information by using a session ID. When users make a request without a session
ID, ASP.NET creates a session ID and sends it with every request and response to the same
user.
2. Application State:
The ASP.NET application is the collection of all web pages, code and other files within a single
virtual directory on a web server. When information is stored in application state, it is available
to all the users. To provide for the use of application state, ASP.NET creates an application
state object for each application from the HTTPApplicationState class and stores this object in
server memory. This object is represented by class file global. Sax. Application State is mostly
used to store hit counters and other statistical data, global application data like tax rate, discount
rate etc. and to keep the track of users visiting the site.
Caching in asp.net:
Caching is a technique of storing frequently used data/information in memory, so that, when
the same data/information is needed next time, it could be directly retrieved from the memory
instead of being generated by the application.
Caching is extremely important for performance boosting in ASP.NET, as the pages and
controls are dynamically generated here. It is especially important for data related transactions,
as these are expensive in terms of response time.
Caching places frequently used data in quickly accessed media such as the random-access
memory of the computer. The ASP.NET runtime includes a key-value map of CLR objects
called cache. This resides with the application and is available via the HttpContext and
System.Web.UI.Page.
In some respect, caching is similar to storing the state objects. However, the storing information
in state objects is deterministic, i.e., you can count on the data being stored there, and caching
of data is nondeterministic.
ASP.NET provides the following different types of caching:
Output Caching : Output cache stores a copy of the finally rendered HTML pages or part of
pages sent to the client. When the next client requests for this page, instead of regenerating the
page, a cached copy of the page is sent, thus saving time.
Data Caching : Data caching means caching data from a data source. As long as the cache is
not expired, a request for the data will be fulfilled from the cache. When the cache is expired,
fresh data is obtained by the data source and the cache is refilled.
Object Caching : Object caching is caching the objects on a page. The cached data is stored in
server memory.
Class Caching : Web pages or web services are compiled into a page class in the assembly,
when run for the first time. Then the assembly is cached in the server. Next time when a request
is made for the page or service, the cached assembly is referred to. When the source code is
changed, the CLR recompiles the assembly.
Configuration Caching : Application wide configuration information is stored in a
configuration file. Configuration caching stores the configuration information in the server
memory.