Sunday, January 22, 2012

XML Data Model

To parse a XML string it is very important to correctly understand the data model of XML here is my understanding of the same

XMLNode is a basic object in a DOM tree
XMLDocument class extends the node class and support methods for performing operations on the document as a whole

A node can have multiple childs nodes below it however each node would only have one parent. Each node can have multiple name-value pairs which are known as Attributes. If an application does not require the structure and editing capabilities provided by DOM then XMLReader and XMLWrite classes can be used as they are faster and are meant to provide a non-cached, forward only access to an XML stream.

Further to this a node could be of different types. Identifying the node type helps to determine what actions can be performed and what properties can be set or retrieved. To understand the different types of nodes lets use the example below

There is another common question that i have encountered as to what is the difference between a node and an element as we will see in the example below that an element is infact a node type

Example
<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>test with a child element <more/> stuff</Item>
  <Item>test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with a char entity: &#65;</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

InputOutputNode Type
<?xml version="1.0"?><?xml version='1.0'?>XmlNodeType.XmlDeclaration
<!-- This is a sample XML document --><!--This is a sample XML document -->XmlNodeType.Comment
<!DOCTYPE Items [<!ENTITY number "123">]><!DOCTYPE Items [<!ENTITY number "123">]XmlNodeType.DocumentType
<Items><Items>XmlNodeType.Element
<Item><Item>XmlNodeType.Element
Test with an entity: &number;</Item>Test with an entity: 123XmlNodeType.Text
</Item></Item>XmlNodeType.EndElement
<Item><Item>XmNodeType.Element
test with a child element test with a child element XmlNodeType.Text
<more><more>XmlNodeType.Element
stuffstuffXmlNodeType.Text
</Item></Item>XmlNodeType.EndElement
<Item><Item>XmlNodeType.Element
test with a CDATA section test with a CDATA section XmlTest.Text
<![CDATA[<456>]]><![CDATA[<456>]]>XmlTest.CDATA
defdefXmlNodeType.Text
</Item></Item>XmlNodeType.EndElement
<Item><Item>XmlNodeType.Element
Test with a char entity: &#65;Test with a char entity: AXmlNodeType.Text
</Item></Item>XmlNodeType.EndElement
<!-- Fourteen chars in this element.--><--Fourteen chars in this element.-->XmlNodeType.Comment
<Item><Item>XmlNodeType.Element
1234567890ABCD1234567890ABCDXmlNodeType.Text
</Item></Item>XmlNodeType.EndElement
</Items></Items>XmlNodeType.EndElement



No comments: