|
|
| 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 a XML document.
|
| 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
address.xml#xpointer (/descendant::streetnumber[@id=9])
So the above XPOINTER points street number=9 in “address.xml”.
|
| What is an XMLReader Class? |
It is an abstract class available from 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.
|
| What is XMLTextReader? |
|
|
The “XmlTextReader” class helps to provide fast access to streams of XML data in a forwardonly and read-only manner. It also checks if the XML is well formed. However, XMLTextReader does not validate against a schema or DTD for that you will need “XmlNodeReader” or “XmlValidatingReader” class.
Instance of “XmlTextReader” can be created in number of ways. For example if you want to load file from a disk you can use the below snippets.
XmlTextReader reader = new XmlTextReader (filename);
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
XmlTextReader reader = new XmlTextReader (file);
While (reader. Read())
{
// your logic goes here
String pdata = reader. Value
}
// Close the stream
Reader. Close ();
To read the content of the current node on which the reader object is you use the “value” property. As shown in the above code “pdata” gets the value from the XML using “reader.Value”.
|
|
| How do we access attributes using “XmlReader”? |
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 “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)
{
while(reader.MoveToNextAttribute())
{
// your logic goes here
string pdata = reader.Value
}
}
reader.MoveToElement();
|
| What does XmlValidatingReader class do? |
|
XmlTextReader class does not validate the contents of an XML source against a schema. The correctness of XML documents can be measured by two things is the document well formed and is it valid. Well-formed means that the overall syntax is correct. Validation is much deeper which means is the XML document is proper w.r.t schema defined.
Therefore, the XmlTextReader only checks if the syntax is correct but does not do validation. There is where XmlValidatingReader class comes in to picture. Therefore, this again comes at a price as XmlValidatingReader have to check for DTD and Schema’s that’s the reason they are slower compared to XmlTextReader.
|
|
|
|
|
| Prev |