• No results found

Deccansoft Software Services – MS.NET XML

In document Windows (Page 73-78)

XML is an universal specifation / standard by W3C providing context / description and structure to the otherwise ordinary text data.

XML can be used for exchanging information between objects developed in different programming language and running on discreate platforms and even if the objects are separed by the firewall.

Yes Yes

Further processing

No No

Error Rules for an XML DOC to be Well Formed

• It should have only one root element tags in XML .These tags are called ‘Root Tags’.

• Tags are case sensitive and hance the opening and closing tag case must match.

• Every tag must be closed. ex:<br></br> or <br/>

• Tags must not overlap each other.

• The value of attributes must be encosed in quotes. Ex: RollNo=”104”

An HTML document which is well formed is called XHTML

An XML Schema document contains declarations of various XML constructs including elements attridutes etc.

This declaration define the syntax and the restrictions while using them in an instance document.

Structure of XML Document (HAS A – Relationship) Document

Processing Instruction.

Comments.

Root Element.

Element.

Processing Instruction (Iinstruction either to the parser or to the application processing the xml)

<?xml version=”1.0” encoding=”UTF-8”?>

<?document format=”rft”?>

Comment.

Text.

CData Section. - <![CDATA[a<b]]> -Unparsed Charater Data.

Entity References. (&lt; / &gt; / &quot; / &amp; / &apos;) Student.xml

<Students>

<Student RollNo="101">

<Name>S1</Name>

<Result>Passed</Result>

</Student>

<Student RollNo="104">

<Name>S4</Name>

<Result>Failed</Result>

</Student>

<Student RollNo="105">

<Name>S5</Name>

<Result>Passed</Result>

</Student>

</Students>

Students.xml

<Students>

<Student RollNo="101">

<Name>

<FirstName>AA</FirstName>

<LastName>XX</LastName>

</Name>

<Result>Passed</Result>

</Student>

<Student RollNo="102">

<Name>

<FirstName>BB</FirstName>

<LastName>YY</LastName>

</Name>

<Result>Failed</Result>

</Student>

XML Schema Document / DTD

Is valid Well

formed XML Instance

Document

Deccansoft Software Services – MS.NET XML

</Students>

XML DOM (Document Object Model)

• XML DOM is a collection of objects created using the various constructs in an XML document.There objects are arranged in a “Tree” like structure

• Using DOM we can read the data from XML document and can also we can create an XML document from scratch or edit an existing document.

• XMLNode is the parent class of all XML classes represending various constructs in an XML document.

XML DOM Imports System.Xml

Dim doc As XmlDocument Dim enStuds As XmlElement

Private Sub btnShow_Click(. . .) Handles btnShow.Click doc = New XmlDocument

'doc.PreserveWhitespace = True doc.Load("..\..\Student.xml") Dim enStuds As XmlElement enStuds = doc.DocumentElement Dim enStud As XmlElement

enStud = CType(enStuds.ChildNodes(1), XmlElement) MsgBox(enStud.GetAttribute("RollNo"))

Dim anRollNo As XmlAttribute

anRollNo = enStud.Attributes("RollNo") MsgBox(anRollNo.Value)

Dim enName As XmlElement

enName = CType(enStud.FirstChild, XmlElement) Dim tnName As XmlText

tnName = CType(enName.FirstChild, XmlText) MsgBox(enStuds.OuterXml)

End Sub

Private Sub Form1_Load(. . .) Handles MyBase.Load doc = New XmlDocument()

'doc.PreserveWhitespace = True doc.Load("..\..\Student.xml") enStuds = doc.DocumentElement Dim enStud As XmlElement

For Each enStud In enStuds.ChildNodes

cmbId.Items.Add(enStud.GetAttribute("RollNo")) Next

End Sub

Private Sub cmbId_SelectedIndexChanged(. . .) Dim enStud As XmlElement

enStud = CType(enStuds.ChildNodes(cmbId.SelectedIndex), XmlElement) txtName.Text = enStud.FirstChild.FirstChild.Value

If (enStud.LastChild.FirstChild.Value = "Passed") Then rbnPassed.Checked = True

Else

rbnFailed.Checked = True End If

End Sub

Private Sub btnNames_Click(. . .) Handles btnNames.Click Dim nlNames As XmlNodeList

Deccansoft Software Services – MS.NET XML

Dim s As String = ""

For Each enName As XmlNode In nlNames

If (enName.NextSibling.FirstChild.Value = "Passed") Then s &= enName.FirstChild.Value & vbCrLf

End If Next MsgBox(s) End Sub

Private Sub btnAdd_Click(. . .) Handles btnAdd.Click Dim enStud, enName, enResult As XmlElement Dim tnName, tnResult As XmlText

Dim anId As XmlAttribute

enStud = doc.CreateElement("Student") enName = doc.CreateElement("Name") enResult = doc.CreateElement("Result") tnName = doc.CreateTextNode(txtName.Text) If (rbnPassed.Checked) Then

tnResult = doc.CreateTextNode("Passed") Else

tnResult = doc.CreateTextNode("Failed") End If

anId = doc.CreateAttribute("RollNo") anId.Value = cmbId.Text

enStuds.AppendChild(enStud) enStud.AppendChild(enName) enStud.AppendChild(enResult) enName.AppendChild(tnName) enResult.AppendChild(tnResult) enStud.SetAttributeNode(anId) doc.Save("..\..\Student.xml") cmbId.Items.Add(cmbId.Text) End Sub

Private Sub btnRemove_Click(. . .) Handles btnRemove.Click Dim enStud As XmlElement

enStud = CType(enStuds.ChildNodes(cmbId.SelectedIndex), XmlElement) enStud.ParentNode.RemoveChild(enStud)

doc.Save("../Student.xml")

cmbId.Items.Remove(cmbId.Text) End Sub

Working with Dataset TYPES OF XML ELEMENTS:

Complex Type: An element which has either attribute or child elements is a complex type of element.

SimpleType: If it is not complex then it is a simple type of element All attributes are always simple type

When the XML document is loaded into DataSet then every ComplexType is loaded Data Table and the simple types with in it are loaded as DataColumns.

If one complex type is nested with in the other complex type then a DataRelation object is created using those two Complex types / DataTables. If they do not have any common field between them, then a column in the format of

<parenttable>_Id is added to both the Datatables.

Ex: Student(RollNo,Name,Student_Id) - DataTable Name(FirstName,LastName,Student_Id) – DataTable Student_Name – DataRelation

Deccansoft Software Services – MS.NET XML

Private Sub btnStudentDS_Click(. . .) Handles btnSimpleDS.Click Dim ds As New DataSet()

'Dim strXML As String 'strXML = ""

'ds.ReadXml(New IO.StringReader(strXML)) ds.ReadXml("../../Student.xml")

Dim dr As DataRow

dr = ds.Tables("Student").NewRow dr("RollNo") = cmbId.Text

dr("Name") = txtName.Text If (rbnPassed.Checked) Then dr("Result") = "Passed"

Else

dr("Result") = "Failed"

End If

ds.Tables("Student").Rows.Add(dr) ds.WriteXml("../../Student.xml") End Sub

Private Sub btnStudentsDS_Click(. . .) Handles btnDS.Click Dim ds As New DataSet()

ds.ReadXml("..\..\Students.xml") Dim drStud As DataRow

Dim s As String = ""

For Each drStud In ds.Tables("Student").Rows Dim name As String

Dim drsName As DataRow()

drsName = drStud.GetChildRows("Student_Name")

name = CStr(drsName(0)("FirstName")) & vbTab & CStr(drsName(0)("LastName"))

s &= CStr(drStud("RollNo")) & vbTab & name & vbTab & CStr(drStud("Result")) & vbCrLf Next

MsgBox(s) End Sub

XML DataDocument Private Sub btnXmlDataDocument_Click(. . .)

Dim ds As New DataSet() ds.ReadXml(“..\..\Students.xml")

Dim dd As New XmlDataDocument(ds) MsgBox(dd.DocumentElement.OuterXml) End Sub

XMLDataDocument is used to construct an XML Dom Tree from the data already existing in DataSet.

It’s a class Inherited fromXMLDocument, thus this has all the properties and methods which the

XMLDocument class has.

XMLTextWriter and XMLTextReader

XMLTextReader and XMLTextWriter are strem based and can be used for situation in which an XML Document needs to be parsed only once. This way of parsing the document is very fast when compared to DOM API as the complete document doesn’t have to loaded into memory at once. This is also because XMLTextReader uses Linear Parsing and Forward Only Parsing.

Private Sub btnXmlTextWriter_Click(. . .) Handles btnXmlTextWriter.Click Dim tw As New XmlTextWriter("c:\demo.xml", Nothing)

tw.WriteStartDocument() tw.WriteStartElement("Root")

tw.WriteAttributeString("att", "attvalue") tw.WriteElementString("E1", "TextInE1")

Deccansoft Software Services – MS.NET XML

tw.WriteEndDocument() tw.Close()

End Sub

Private Sub btnTextReader_Click(. . .) Handles btnTextReader.Click Dim xr As New XmlTextReader("../../Student.xml")

Dim strNames As String = ""

Dim strCurrentName As String = ""

While (xr.Read())

If (xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "Name") Then xr.Read()

strCurrentName = xr.Value

ElseIf (xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "Result") Then xr.Read()

If (xr.Value = "Passed") Then

strNames &= strCurrentName & vbCrLf End If

End If End While

MsgBox(strNames) End Sub

XPath

XPath is a specification for framing of Path so that we can get the reference to nodes in XMLDocument based on their values.

Private Sub btnGetAllNodes_Click(. . .) Dim nl As XmlNodeList

nl = doc.SelectNodes(txtPath.Text) Dim str As String = ""

For Each n As XmlNode In nl str &= n.Name & " - "

& n.OuterXml & vbCrLf Next

MsgBox(str) End Sub

Arithemetic Operators: +, - , *, div Logical Operators: and, or, not

Sample XPaths:

• E1/E2: All occurences of E2 which are immidiate child of E1.

• E1/*/E2: All occurences of E2 which are grand children of E1 or the ancistor of E1.

• E1//E2: All occurances of E2 which has E1 as ancestor.

• //E1: All occurances of E1 any where in the document.

• E1[1]: First occurance of E1.

• E1[last()]: Last occurance of E1.

• E1[E2]: All occurance of E1 which has E2 as child.

• E1[E2 and E3 ]: All occurance of E1 which has both E2 and E3 as child.

• E1[E2 or E3 ]: All occurance of E1 which has either E2 or E3 as child.

• E1[not(E2)]: All occurance of E1 where E2 is not a Child.

• E1/@A1: All attributes by name A1 of E1 elements.

• E1/@*: All attributes of all occurances element E1.

• E1/[@A1=’1’]: All occurances of E1 where attributes A1=1.

• E1/text(): Returns all the text nodes under E1.

• E1/comment(): Returns all comment nodes under E1

In document Windows (Page 73-78)