Bc0053 Vb.net & XML

20
Fall 2012 Bachelor of Computer Application (BCA) – Semester 5 BC0053 – VB.Net & XML – 4 Credits (Book ID: B0975) Assignment Set – 1 (40 Marks) Answer all the questions . Each question carries eight marks (8 x 5 = 40) 1. Describe the process of compiling and running a Visual Basic Applications. Ans : Compiling and Running a Visual Basic Applications : Figure 1-4 shows how an application is compiled and run when using Visual Basic.NET. To start, you use Visual Studio.NET to create a project, which is made of one or more source files that contain Visual Basic statements. Most simple projects consist of just one source file, but more complicated project can have more than one source file. A project may also contain other types of files, such as sound files, image files, or simple text files. As the figure shows, a solution is a container for projects, which you’ll learn more about in a moment. You use the Visual Basic complier, which is built into Visual Studio, to compile your Visual Basic source code into Microsoft Intermediate Language (or MSIL). For short, this can be referred to as Intermediate Language(or IL). At this point, the Intermediate Language is stored on disk in a file that’s called an assembly. In addition to the IL, the assembly includes references to the classes that the application requires. The assembly can then be run on any pc that has the

Transcript of Bc0053 Vb.net & XML

Page 1: Bc0053 Vb.net & XML

Fall 2012

Bachelor of Computer Application (BCA) – Semester 5

BC0053 – VB.Net & XML – 4 Credits (Book ID: B0975)

Assignment Set – 1 (40 Marks)

Answer all the questions . Each question carries eight marks (8 x 5 = 40)

1. Describe the process of compiling and running a Visual Basic Applications.

Ans :

Compiling and Running a Visual Basic Applications :

Figure 1-4 shows how an application is compiled and run when using Visual Basic.NET. To start, you use Visual Studio.NET to create a project, which is made of one or more source files that contain Visual Basic statements. Most simple projects consist of just one source file, but more complicated project can have more than one source file. A project may also contain other types of files, such as sound files, image files, or simple text files. As the figure shows, a solution is a container for projects, which you’ll learn more about in a moment.

You use the Visual Basic complier, which is built into Visual Studio, to compile your Visual Basic source code into Microsoft Intermediate Language (or MSIL). For short, this can be referred to as Intermediate Language(or IL).

At this point, the Intermediate Language is stored on disk in a file that’s called an assembly. In addition to the IL, the assembly includes references to the classes that the application requires. The assembly can then be run on any pc that has the Common Language Runtime installed on it. When the assembly is run, the CLR converts the Intermediate Language to native code that can be run by the Windows operating system.

CLR is available for Unix System also, i.e. Mono. It is possible that the CLR will eventually be available for other operating system as well. In other words, the Common Language Runtime makes platform independence possible. Visual Basic application will be able to run on those operating systems as well as Windows/Unix operating systems. Whether this will happen and how well it will work remains to be seen.

Page 2: Bc0053 Vb.net & XML

2 . Explain various basic data types and variables in visual .

Ans :

Basic Data Types and their mapping to the CTS(Common Type System):

There are two kinds of data types in VB.Net

1. Value type (implicit data types, Structure and Enumeration)

2. Reference Type (objects, delegates)

Value types are passed to methods by passing an exact copy while Reference types are passed to methods by passing only their reference(handle). Implicit data types are defined in the language core by the language vendor, While explicit data types that are made by using or composing implicit data types.

As seen in the first unit, implicit data types in .net compliant languages are mapped to types in Common Type System (CTS) and CLS (Common Language Specification). Hence, each implicit data type in VB.Net has its corresponding .Net type. The implicit data types in VB.Net are:

VB.Net type Corresponding.Net type

Size in bytes

Description

Boolean Boolean 1 Contains either True or False

Char Char 2 Contains any single Unicode character enclosed in double quotation marks followed by a c, for example “x”c

Integral types

Byte Byte 1 May contain integers from 0-255

Short Int16 2 Ranges from -32,768 to 32,767

Integer (default)

Int32 4 Range from -2,147,483,648 to 2,147,483,647

Long Int64 8 Ranges form – 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Page 3: Bc0053 Vb.net & XML

Floating point types

single single 4 Ranges from ± 1.5 × 10-45 to ± 3.4 × 1038 with 7 digits precision. Requires the suffix ‘f’ or ‘F’

Double(default) Double 8 Ranges form ±5.0 × 10-324 to ±1.7 × 10308 with 15-16 digits precision.

Decimal Decimal 12 Ranges from 1.0 × 10-28 to 7.9 × 1028 with 28-29 digits precision. Requires the suffix ‘m’ or ‘M’

Implicit data types are represented in language using ‘keywords’; so each of above is a keyword in VB.Net (Keyword are the words defined by the language and can not be used as identifiers). It is worth-nothing that string is also an implicit data types in VB.Net, so String is a keyword in VB.Net. Last point about implicit data types is that they are value types and thus stored at the stack, while user defined types or referenced types are stored at heap. Stack is a data structure that store items in last in first out (LIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. Heap is the total memory available at run time. Reference types are allocated at heap dynamically (during the execution of program). Garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.

Variables :

During the execution of program, data is temporarily stored in memory. A variable is the name given to a memory location holding particular type of data. So, each variable has associated with it a data type and value. In VB.Net, a variable is declared as:

Dim <variable> as <data type>

Example:

Dim I As Integer

The above line will reserve an area of 4 bytes in memory to store integer type values, which will be referred in the rest of program by identifier ‘I’. You can initialize the variable as you declare it (on the fly) and can also declare/initialize multiple variable of same type in a single statement.

Examples:

Dim isReady As Boolean = True

Dim percentage = 87.88, average = 43.9 As Single

Dim digit As Char = “7”c

Page 4: Bc0053 Vb.net & XML

3. With the help of suitable example, describe the development of single documentsand multi-document interface .

Ans :

Single-document and multiple-document interfaces :

Figure 4.17 shows two versions of the financial calculation application that will be used as examples in this chapter. These applications let the user calculate an investment or depreciation using forms like the ones you saw in the last chapter. In addition, each application includes a third form that provides a way for the user to access the other forms.

The first version of this application uses a single-document interface, or SDI. In an SDI application, each form runs in its own application window, and this window is usually shown in the windows taskbar. Then, you can click on the buttons in the taskbar to switch between the open forms. When you use this interface, each form can have its own menus and toolbars. In addition, a main form called a startup form typically provides access to the other forms of the application. In this figure, for example, the startup form includes buttons that the user can click on to display the other forms.

The second version of this application uses a multiple-document interface, or MDI. In an MDI application, a container from called a parent form contains one or more child forms. Then, the menus and toolbars on the parent form contain the commands that let you open and view forms, and you can use its window menu to switch between the open forms. When you close the parent form of an MDI application, all of the child forms are closed and the application ends.

The main advantage of a multiple-document interface is that the parent form manages multiple instances of child forms for you. In constant, If you crate multiple instances of a form is an SDI application, you have to manage them yourself. As you can imagine, that can get un widely.

The parent form in the MDI application in this figure also includes a status bar. A bar like this can be set up to display a number of information items. Although you can add a status bar to any form either an SDI or MDI application, it makes the most sense to use on the parent form in an MDI application.

Incidentally, you can also develop an explorer-style interface with Visual Basic.NET. In this type of interface, a single window is split into two panes just as it is in the Window Explorer. Then, you can use the left pane to navigate between different parts of the application, and you can use the right pane to work with the application. This type of interface, however, isn’t presented in this book.

Page 5: Bc0053 Vb.net & XML

4. With the help of suitable example, describe the development of single documentsand multi-document interface .

Ans:

Same answer to previous question.

Page 6: Bc0053 Vb.net & XML

5. Explain the concept of setting a connection string with example .

Ans :

Setting a Connection String :

There are properties and methods associated with the Connection Object, of course. We want to start with the Connection String property. This can take Many parameters. Fortunately, we only need a few of these.

We need to pass two things to our new Connection Object: the technology we want to use to do the connecting to our database; and where the database is. (if your database was password and user name protected, you would add these two parameters as well. Ours isn’t, so we only need the two.)

The technology is called the Provider; and you use “Data Source” to specify where your database is. This should be entered on the same line, and not two as it is below. So add this to your code:

con.ConnectionString = “PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = C:\AddressBook.mdb”

Notice the two parts, separated by a semi-colon:

1st Part: PROVIDER=Microsoft.Jet.OLEDB.4.0

2nd part: Data Source = C:\AddressBook.mdb

The first part specifies which provider technology we want to use to do the connecting (JET). The second part, typed after a semi-colon, points to where the database is. In the above code, The database is on the C drive, in the root folder. The name of the Access file we want to connect to is called AddressBook.mdb. (Note that “Data Source” is two words, and not one.)

But your coding window should now look like this:

Private Sub btnLoad_Click(ByVal Sender as Object,_

ByVal e as System.EventArgs)_

Handles btnLoad.Click

Dim con as new OleDbConnection

Con.ConnectionString = “Provider = Microsoft.Jet.OLEDB.4.0;

DataSource = C:\AddressBook.mdb”

This assumes that you have copied the Address Book database over to the root folder of your C Drive. If you’ve copied it to another folder, change the “Data Source” part to the match. For example, if you copied it to a folder called “databases” you’d put this:

Data Source = C:\databases\AddressBook.mdb

In our code , though, Connection String is a property of the con variable. The con Variable holds our Connection Object. We’re passing the Connection String the name of a data provider, and a path to the database.

Page 7: Bc0053 Vb.net & XML

Fall 2012

Bachelor of Computer Application (BCA) – Semester 4

BC0053 – VB.Net & XML – 4 Credits (Book ID: B0975)

Assignment Set – 2 (40 Marks)

Answer all the questions . Each question carries eight marks (8 x 5 = 40)

1. What are syntax rules for writing XML file ? Explain XML entities.

Ans :

XML Syntax Rules

The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use.

1. XML consist of user-defined tags.

Ex : < address>Hyderabad </address>

2. We can describe the XML with the tags.

3. In XML file “ each & every tag “ must have an “ ending tags “

Ex: <student> </student>

To create the empty tags we use a single tag or a single line statement.

< student /> ( i.e., < student > < /student > )

4. XML is “case sensitive “

Ex 1: The below is the invalid XML file, because the in the starting element (i.e <student>) ‘s’ is small and in the ending tag ‘S’ is capital.

< student > < /Student >

5. We can’t create an XML file without a root element .

Ex: In the below student.xml file <student> is the root element.

<student>

<sno>1</sno>

<sname>a</sname>

<saddr>Hyderabad</saddr>

</student>

6. In XML we need to use only proper nesting.

Ex: The below is the valid XML file

<student>

<sno>1</sno>

</student>

7. In XML we use the comments as follows

<! - -Comment - ->

Page 8: Bc0053 Vb.net & XML

8. When we are declaring the attributes each and every attribute value must be enclosed with in the single quotes (or) double quotes.

Ex: <student sno=”1”> </student>

(or)

<student sno=’1’></student>

9. Element: An element consist of

a) Simple Content

b) Mixed Content

c) Complex Content

a) Simple Content: Simple content consists of some text in between the tags.

Ex: <address> this is address</address>

b) Mixed Content: Mixed content consists of simple content and attributes.

Ex:

<address attr=”some_value”> this is address </address>

c) Complex Content: Comple content consist of another tag

Ex:

<student>

<sno>1</sno>

<sname>aaa</sname>

<marks>23</marks>

<address>bb</address>

</student>

Rules in Creating the Element names

1. Names with an underscore are nice : < first_name >, < last_name >

2. Names should be short and simple, like this <book _title > not like this

< the_title_of_the_book >.

3. Avoid “_ “characters. If you name some thing “first_name”, some software’s may thing you want to subtract name from first.

4. Avoid “ : “ characters. Colons are reserved to be used for some thing called name spaces.

5. Avoid “. “Characters. If you name some thing “first. name “, some software’s may think that “ name “ is a property of the object “ first “.

XML EntitiesEntities are variables used to define shortcuts to standard text (or) special characters.

Entity references are references to entities.

Entities can be declared internal (or) external.

Some characters have a special meaning in XML, like the less than sign (<) that defines the start of an XML tag. The following entities are predefined in XML:

Entity Character

Page 9: Bc0053 Vb.net & XML

References

&It; <

&gt; >

&amp; &

&quot; “

&apos; ‘

Figure : Entities

Entities can be declared either internal (or) external.

 Internal Entity declaration:

This type of entities are declared in DTD document and accessed in the XML document.

Here the content that the entity has to represent is specified in declaration part of the entity i.e. with in the DTD document, thus these types of entities are known as internal entities.

Syntax:

<! ENTITY entity_name “entity_value”>

Syntax to refer the entity:

& entity_name;

XML Example:

< ! ENTITY writer “hai”>

<! ENTITY copyright “copyright.yahoo”>

Valid XML Example:

<author> &writer; &copyright; </author>

 External entity:

In this case the content that is to be referred by entity is placed into separated document instead of specifying into the same document.

Syntax:

<! ENTITY entity_name SYSTEM “file path locating the document containing the content to refer”>

Example:

<! ENTITY writer SYSTEM “entities.dtd”>

XML Example:

<author> &writer ;</author>

Page 10: Bc0053 Vb.net & XML

2. What are the method of properties of XML Dom ? Explain with examples .

Ans :

XML DOM - Properties and Methods

Properties and methods define the programming interface to the XML DOM.

The DOM models XML as a set of node objects. The nodes can be accessed with JavaScript or other programming languages. In this tutorial we use JavaScript.

The programming interface to the DOM is defined by a set standard properties and methods.

Properties are often referred to as something that is (i.e. nodename is "student").Methods are often referred to as something that is done (i.e. delete "student").

XML DOM Methods:

1. x.getChildNodes

Returns a list of all of the children of the current node. This is returned as a Node List object which has its own methods.

2. x.getFirstChild

Returns the first child of the current node.

3. x.getLastChild

Returns the last child of the current node.

4. x.getPreviousSibling

Returns the node immediately before the current node

5. x.getNextSibling

Returns the node immediately after the current node

6. x.getAttributes

Returns a NamedNodeMap containing the attributes of the current node.

7. x.insertBefore (newnode, refnode)

Inserts the new node immediately before the current node which is passed as the refnode parameter.

8. x.replaceNode (newnode, oldnode)

Replaces the node in its second parameter with that in its first.

9. x.removeChild (child)

Removes the child node from the tree.

10. x.appendChild (child)

Appends the child to the end of the list of children of the current node.

11. x.getElementsByTagName (“tag”)

Returns all elements which have the name supplied as a parameter. To return all of the elements in a tree, use the parameter “*”. The parameter is a string which must be quoted.

Page 11: Bc0053 Vb.net & XML

XML DOM Properties:

These are some typical DOM properties:

x.nodeName - the name of x

x.nodeValue - the value of x

x.parentNode - the parent node of x

x.childNodes - the child nodes of x

x.attributes - the attributes nodes of x

Examples

Example 1: Access a node using its index number in a node list. This example uses the getElementsByTagname () method to get the second <sname> element in "college.xml”

<html>

<head>

<script type="text/javascript" src="loadxmldoc.js"></script>

</head>

<body>

<script type="text/javascript">

xmlDoc=loadXMLDoc("college.xml");

x=xmlDoc.getElementsByTagName("sname");

document.write(x[2].childNodes[0].nodeValue);

</script>

</body>

</html>

OUTPUT:

Rani

Page 12: Bc0053 Vb.net & XML

3. Explain XML schema. What are the disadvantages of DTD?

Ans :

XML Schema:

XML Schema includes constructors that help us to prepare XML markup language specifications, alternative to DTD. The XML Schema language is also referred to as XML Schema Definition (XSD).

The disadvantages of DTD:

While specifying the child elements in DTD it can only use /,* (or) + to describe the occurance. But in most of the cases we want to describe the exact number for the lower and upper limit. Like we may want the emps element to have minimum 5 emp elements and maximum 20.

DTD does not have a support for common and simple types instead it provides only PCDATA.

DTD does not support XML namespace.

In case of DTD the element definition name and the element name is same.

Page 13: Bc0053 Vb.net & XML

4. What is XPath ? Explain Xpath string functions with examples?

Ans:

XPath:

XPath specification is a part of xml specification

XPath specification include some constructs to build paths, expressions and include some functions for string and operations.

These specification can be used in XSL.

XPath is a major element in XSL.

XPath is a W3C recommendation.

XPath String functions:

1. Substring(string, startindex, len)

2. Substring-before(string1string2)

Example: substring-before(‘abc’,’b’)It returns ‘a’.

3. Substring-after(string1string2)

Example: substring-before(‘abc’,’b’)It returns ‘c’.

4. Contains(string1,string2): Returns Boolean. This returns true if string2 appears in string1, otherwise false.

5. Startwith(string1,string2);

Example: startwith(‘abc’,’ab’) It returns true.

6. String-length(string): It returns the length of a string

7. Normalize-space (string): Returns the input string after normalize the white space, which includes eliminating white spaces in beginning and ending and remove multiple white space identified in between of the string, replacing with a single white space.

Example: - - abc- - xyz -

In the above the dashes (-) are white spaces.Output: ‘abc-xyz’

Page 14: Bc0053 Vb.net & XML

5. What is Dom ? Draw the Dom tree for emp.xml.

Ans :

 XML DOM Node Tree

In the DOM, every thing in an XML document is a node.

The DOM says:

The entire document is a document node.

Every XML element is an element node.

The texts in the XML element are text nodes.

Every attribute is an attribute node.

Comments are comment nodes.

The XML DOM views an XML document as a tree-structure. The tree structure is called a node-tree. All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created. The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree.

Page 15: Bc0053 Vb.net & XML

The Dom tree for emp.xml

 

Parent Child

Root element <company>

Attribute“firstname”

Element<employee>

Attribute “dept”

Element<empname>

Element<empno>

Element <job>

Element <desc>

Element <salary>

Element <deptno>

Element<deptname>

Text: d100

Text: d100

Text: Arup

Text: e124

Text: SE

Text: Software

Text: 5000