# XML Interview Questions in .NET

We have compiled most frequently asked .NET Interview Questions which will help you with different expertise levels.

## .NET Interview Questions on XML

Note: In this chapter, we will first just skim through basic XML interview questions so that you do not
get stuck up with simple questions.

Question 1.
What is XML? (B)What is XML?
XML (Extensible Markup Language) is all about describing data. Below is a XML, which describes invoice data.

<?xml version="1.0 " encoding=''ISO-8859-1" ?>
<invoice>
<productname>Shoes</productname>
<qty>12 </qty>
<totalcost>100</totalcost>
<discount>10</discount>
</invoice>

An XML tag is not something predefined but it is something you have to define according to your needs. For instance, in the above example of the invoice, all tags are defined according to business needs. The XML document is self-explanatory; anyone can easily understand looking at the XML data what exactly it means.

Question 2.
What is the version information in XML?
“Version” tag shows which version of XML is used.

Question 3.
What is the ROOT element in XML? (B)What is the ROOT element in XML?
In our XML sample given previously,<invoice></invoice> tag is the root element. Root element is the top most elements for a XML.

Question 4.
If XML does not have a closing tag will it work?
No, every tag in XML, which is opened, should have a closing tag. For instance, at the top, if I remove the </discount> tag that XML will not be understood by a lot of applications.

Question 5.
Is XML case-sensitive?
Yes, they are case-sensitive.

Question 6.
What is the difference between XML and HTML?
XML describes data while HTML describes how the data should be displayed. Therefore, HTML is about displaying information while XML is about describing information.

Question 7.
Is XML meant to replace HTML?
No, they both go together one is for describing data while the other is for displaying data.

Question 8.
Can you explain why your project needed XML?

Note: This is an interview question where the interviewer wants to know why you have chosen XML.

Remember XML was meant to exchange data between two entities as you can define your user-friendly’ tags with ease. In real-world scenarios, XML is meant to exchange data. For instance, you have two applications that want to exchange information. However, because they work in two completely opposite technologies it is difficult to do it technically. For instance, one application is made in Java and the other in. NET. However, both languages understand XML so one of the applications will spit XML file, which will be consumed and parsed by other applications

You can give a scenario of two applications, which are working separately, and how you chose XML as the data transport medium.

Question 9.
What is DTD (Document Type Definition)?
It defines how your XML should structure. For instance, in the above XML, we want to make it compulsory to provide “qty” and “total cost”, also that these two elements can only contain numeric. Therefore, you can define the DTD document and use that DTD document within that XML.

Question 10.
What is well-formed XML?
If an XML document is confirming to XML rules (all tags started are closed, there is a root element, etc) then it is a well-formed XML.

Question 11.
What is a valid XML?
If XML is confirming DTD rules then it is a valid XML.

Question 12.
What is the CDATA section in XML?
All data is normally parsed in XML but if you want to exclude some elements, you will need to put those elements in CDATA.

Question 13.
What is XSL?
XSL (extensible Style Sheet) Language is used to transform XML documents into some other document. Therefore, its transformation document can convert XML to some other document. For instance, you can apply XSL to XML and convert it to HTML documents or probably CSV (Comma Separated Value) files.

Question 14.
What is element and attributes in XML?
In the below example invoice is the element and the in number the attribute.

<invoice in number=1002x/invoice>

Question 15.
Which are the namespaces in .NET used for XML?
“System.xml.dll” is the actual physical file, which has all XML implementation. Below are the commonly used namespaces:

• System.Xml
• System.Xml.Schema
• System.Xml, XPath
• System.Xml.Xsl

Question 16.
What are the standard ways of parsing XML documents?
XML parser sits in between the XML document and the application that want to use the XML document. Parser exposes a set of well-defined interfaces, which can be used by the application for adding, modifying, and deleting the XML document contents. Now whatever interfaces XML parser exposes should be standard or else that would lead to different vendors preparing their own custom way of interacting with XML documents.

There are two standard specifications, which are very common and should be followed by an XML parser:

DOM (Document Object Model)

DOM Is a W3C (World Wide Web Consortium) recommended way for creating XML documents. In DOM, we load the entire XML document into memory and allows us to manipulate the structure and data of the XML document.

SAX: Simple API for XML

SAX is an event-driven way for processing XML documents. In DOM, we load the whole XML document into memory and then the application manipulates the XML document (See Figure 18.1). However, this is not always the best way to process large XML documents, which have huge data elements. For instance, you only want one element from the whole XML document or you only want to see if the XML is proper which means loading the whole XML in memory will be quite resource-intensive. SAX parsers parse the XML document sequentially and emit events like start and end of the document, elements, text content, etc. Therefore, applications that are interested in processing these events can register implementations of callback interfaces. SAX parser then only sends those event messages, which the application has demanded.

Figure 18.2 is a pictorial representation of how the DOM parser works. Application queries the DOM Parser for the “quantity” field. DOM parser loads the complete XML file into memory.

DOM parser then picks up the “quantity” tag from the memory-loaded XML file and returns back to the application.

SAX parser does not load the whole DOM into memory but has an event-based approach. SAX parser while parsing the XML file emits events. For example, in Figure 18.3, its has emitted Invoice tag start event, Amount Tag event, Quantity tag event, and Invoice end tag event. However, our application software is only interested in quantity value. Therefore, the application has to register to the SAX parser saying that he is only interested in the quantity field and not any other field or element of the XML document. Depending on what interest the application software has SAX parser only sends those events to the application the rest of the events is suppressed. For instance in Figure 18.3 only quantity tag event is sent to the application software and the rest of the events are suppressed

Question 17.
In what scenarios will you use a DOM parser and SAX parser?

• if you do not need all the data from the XML file then the SAX approach is much preferred to DOM as DOM can be quite memory intensive. In short, if you need a large portion of the XML document it’s better to have DOM.
• With the SAX parser, you have to write more code than DOM.
• If you want to write the XML into a file, DOM is the efficient way to do it.
• Sometimes you only need to validate the XML structure and do not want to retrieve any Data for those instances SAX is the right approach.

Question 18.
How was XML handled during COM times?
During COM, it was done by using MSXML (Microsoft extensible Markup Language) 4.0. So old > languages like VB6, VC++ used MSXML 4.0, which was shipped with SP1 (Service Pack 1).

Note: This book will not show any samples as such for MSXML 4.0. So if anyone interested please
do refer the same in MSDN and try to compile some sample programs.

Question 19.
What is the main difference between MSML and .NET Framework XML classes?
MSXML supports XMLDOM and SAX parsers while .NET framework XML classes support XML DOM and XML readers and writers.
MSXML supports asynchronous loading and validation while parsing. For instance, you can send synchronous and asynchronous calls to a remote URL. However, as such, there is no direct support „ of synchronous and asynchronous calls in .NET framework XML. However, it can be achieved by using “System.Net” namespaces.

Question 20.
What are the core functionalities in the XML .NET framework? Can you explain in detail those functionalities?
The XML API for the .NET Framework comprises the following set of functionalities:

XML readers: With XML readers, the client application gets a reference to instances of reader class. Reader class allows you to scroll forward through the contents like moving from node to node or element to element. You can compare it with the “SqlDataReader” object in ADO.NET, which is forward only, in short, XML reader allows you to browse through the XML document.

XML writers: Using XML writers, you can store the XML contents to any other storage media. For instance, you want to store the whole in-memory XML to a physical file or any other media.

XML document classes: XML documents provide a in-memory representation for the data in an XMLDOM structure as defined by W3C. It also supports browsing and editing of the document. Therefore, it gives you a complete memory tree structure representation of your XML document.

Question 21.
What is XSLT?
XSLT (extensible Stylesheet Language Transformations) is a rule-based language used to transform XML documents into other file formats. XSLT is nothing but generic transformation rules, which can be applied to transform XML documents to HTML, CS, Rich text, etc.

You can see in Figure 18.4 how the XSLT processor takes the XML file and applies the XSLT transformation to produce a different document.

Question 22.
Define XPath?
It is an XML query language to select specific parts of an XML document. Using XPath, you can address or filter elements and text in an XML document. For instance, a simple XPath expression like “Invoice/ Amount” states find “Amount” node that is Chilean of “Invoice” nodes.

Question 23.
What is the concept of XPointer?
XPointer is used to locate data within XML document. XPointer can point to a particular portion of a XML document, for instance
So the above XPointer points street number=9 in “address .xml”.

Question 24.
It is an abstract class available from the System.XML namespace. XML reader works on a read-only stream browsing from one node to other in a forward direction. It maintains only a pointer to the current node but has no idea of the previous and the next node. You cannot modify the XML document, you can only move forward.

Question 25.
The “XmlTextReader” class helps to provide fast access to streams of XML data in a forward-only and read-only manner. It also checks if the XML is well-formed. However, XMLTextReader does not validate against a schema or DTD (Document Type Definition) for that you will need “XmlNodeReader” or “XmlValidatingReader” class.

The instance of “XmlTextReader” can be created in a number of ways. For example, if you want to load the file from a disk you can use the below snippets.

To loop through all the nodes you need to call the “read ().” method of the “XmlTextreader” object, “read ()” method returns “true” if there are records in the XML document or else it returns “false”.

//Open the stream
{
}
// Close the stream
Reader. Close ( );

To read the content of the current node on which the reader or/ect is you use the “value” property. As shown in the above code “data” gets the value from the XML using “reader . Value”.

Question 26.
How do we access attributes using “XmlReader”?
The below snippets shows the way to access attributes. First in order to check whether there any attributes present in the current node you can use the “HasAttributes” function and use the “MoveToNextAttribute” method to move forward in attribute, in case you want to move to the next element use “MoveToElement () “.

if (reader.HasAttributes)
{
{
}
}
reader.MoveToElement( );

Question 27.
In this section, we will do a simple walkthrough of how to use the “XmlReader” class. Sample for the same is available in both languages (C# and VB.NET) which you can find in “WindowsApplicationXMLVBNET” and “WindowsApplicationCSharp” folders. The task is to load the “TestingXML.XML” file and display its data in a message box. You can find the “TestingXML.XML” file in “BIN” directory of both folders. Figure 18.5 shows are the display of the “TestingXML.XML” file and its content.

Both the projects have the command button “CmdLoadXML” which has the logic to load the XML file and display the data in the message box (See Figure 18.6). I have pasted only the “CmdLoadXML” command button logic for simplicity. Following are the basic steps done:

• Declared the “XMLTextReader” object and gave the XML filename to load the XML data.
• Read the “XMLTextReader” object until it has data and concatenate the data in a temporary string.
• Finally, display the same in a message box.

It holds true for the C# code as shown in Figure 18.7.

Figure 18.8 shows the output.

Question 28.