Module 3
Module 3
with .NET
MODULE - 3 XML, Networking, and 22CSE451.3 8 Hours
Web Services with .NET
To create XML documents in Visual Studio Code, you can follow these
steps:
• Open Visual Studio Code : Launch Visual Studio Code on your computer.
• Create a New File : Click on "File" in the menu bar, then select "New File" to
create a new fi le.
• Save the File with ".xml" Extension : Save the newly created fi le with a
".xml" extension. You can do this by clicking on "File" > "Save As" and then
providing a fi le name with the ".xml" extension.
• Start Writing XML : Once the fi le is created and saved, you can start writing
your XML content. You can use XML syntax to structure your data as needed.
• Edit and Save : You can continue editing your XML document as needed.
Remember to save your changes regularly.
Syntax
<Company>
<Employee>
<FirstName>Tanmay</FirstName>
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Bangalore</City>
<State>Karnataka</State>
<Zip>560212</Zip>
</Address>
</Employee>
What is an XML Element?
An XML element is everything from (including) the element's start tag to
<price>29.99</price>
•text
•attributes
•other elements
<element></element>
XML Attributes
XML elements can have attributes, just like HTML.
Attribute values must always be quoted. Either single or double quotes can be
used.
For a person's gender, the <person> element can be written like this:
<person gender="female">
Or
<person gender='female'>
If the attribute value itself contains double quotes you can use
single quotes, like in this example:
<person >
<gender >female </gender >
<firstname >Anna</firstname >
<lastname >Smith</lastname >
</person >
In the first example, gender is an attribute. In the last example, gender is an element. Both examples
provide the same information.
There are no rules about when to use attributes or when to use elements in XML.
Searching for a Single Node
Searching for a single node in a data structure using C# can vary depending on the type of data structure
you are dealing with. Here are steps for searching a single node in three common data structures: a binary
search tree (BST), a linked list, and a graph using depth-first search (DFS).
Steps:
Compare the target value with the value of the current node.
If the target value is less than the current node's value, move to the left child and repeat from step 2.
If the target value is greater than the current node's value, move to the right child and repeat from step 2.
If you reach a null node, the target value is not in the tree.
2. Linked List
Steps:
Compare the target value with the value of the current node.
If you reach the end of the list (null node), the target value is not in the
list.
3. Graph (using Depth-First Search)
Steps:
Compare the target value with the value of the current node.
If not, recursively search each adjacent (neighbor) node that has not been
visited yet.
If all nodes connected to the start node are visited and the target node is not
found, it does not exist in the graph.
Search Axes
In XML, XPath (XML Path Language) is used to navigate through elements and attributes
in an XML document.
XPath uses a variety of expressions, including axes, to locate specific parts of an XML document.
Axes allow you to specify the direction of navigation relative to the current node.
1. Child Axis (/) : Selects all child nodes of the current node.
Example: /bookstore/book selects all <book> elements that are children of the <bookstore> element.
Example: ../title selects the <title> element that is a parent of the current node.
Ancestor Axis (ancestor::): Selects all ancestors (parent, grandparent, etc.) of the current
node.
Example: ancestor::bookstore selects the <bookstore> element if it is an ancestor of the
current node.
Following Axis (following::): Selects all nodes that come after the current node in
document order.
Example: following::book selects all <book> elements that come after the current node.
Preceding Axis (preceding::): Selects all nodes that come before the current node in
document order.
Example: preceding::book selects all <book> elements that come before the current node.