Wednesday, August 19, 2009

XML document

XMLDom in vb6 equal xmldocument in .net


Loading an XML Document

Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
MessageBox.Show(MyXMLDocument.InnerXml)
MessageBox.Show(MyXMLDocument.InnerText)


Retrieve data from node

To retrieve the data within the Address node, we can specify the following XPath query: /contact/address.


Dim MyXMLDocument As System.Xml.XmlDocument
MyXMLDocument = New System.Xml.XmlDocument()
MyXMLDocument.Load("Contact.xml")
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(“/contact/name/first”)
MessageBox.Show(MyNode.OuterXml)
MessageBox.Show(MyNode.InnerXml)


Count method

Dim MyXpath As String
MyXpath = "/contact/email"
Dim MyNodeList As System.Xml.XmlNodeList
MyNodeList = MyXMLDocument.SelectNodes(MyXpath)
MessageBox.Show(MyNodeList.Count)

Dim x As Integer
For x = 0 To MyNodeList.Count - 1
MessageBox.Show(MyNodeList.Item(x).InnerXml)
Next


Reading attribute

MyXpath = "/contact/address/@city"
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)
MessageBox.Show(MyNode.OuterXml)
MessageBox.Show(MyNode.InnerXml)


Update element

Dim MyXpath As String
MyXpath = "/contact/name/first"
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)
MessageBox.Show(MyNode.OuterXml)
MyNode.InnerText = "Roberto"

Updating attribute


Dim MyXpath As String
MyXpath = "/contact/address/@city"
Dim MyNode As System.Xml.XmlNode
MyNode = MyXMLDocument.SelectSingleNode(MyXpath)
MessageBox.Show(MyNode.OuterXml)
MyNode.InnerText = "Lake Forest"