Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Tuesday, July 3, 2007

Reading XML File Using XMLDocument Object

In the previous blog, I discussed how to read XML file using XMLTextReader object of the System.XML namespace. In the same namespace, we also have XMLDocument which can also be used to read XML files.

You can use the Load method to read an XML file and process the elements using XPath selection if you know the structure of the XML document as follow:

Dim myXmlDoc as New XmlDocument
Dim myNode as XmlNode
myXmlDoc.Load("Cars.xml")
myNode = myXmlDoc.SelectSingleNode("/car/honda")
myTextBox.Text = myNode.InnerXml & vbCrLf

The XmlNode object provides the NextSibling method which can be used to move to the next element or node.

myNode = myNode.NextSibling
myTextBox.Text &= myNode.InnerXml

Now that works fine if you know the the structure of the XML document. If you don't know the structure, you can navigate through the document using FirstChild and NextSibling methods.

myNode = myXmlDoc.FirstChild
While myNode.NodeType <> XmlNodeType.Element And Not myNode Is Nothing
myNode = myNode.NextSibling
End While
myTextBox.Text = myNode.InnerXml

This will dump everything in that node to the textbox including the element names and attributes.

It's not possible to cover all the methods and properties of the XMLDocument and XMLNode in a short blog, but I hope this will get you going.

Further reading should be done on the following topics: XMLDocument, XMLNode, XMLAttribute, FirstChild, HasChildNodes, InnerText, InnerXML...

Download the sample project here.

Friday, June 29, 2007

Reading XML File Using XMLTextReader

System.XML namespace provides us with the XMLTextReader class which we could use to read and process XML files.

The most commonly used attributes are .Name and .Value which returns information of about the elements and their associated information.

MoveToNextAttribute method is used to navigate to the next element. The project below uses an OpenFileDialog box to get the XML file which is then passed to XMLTextReader object and open it. Then the Read method is called to read in and processed the entire XML file and put its content into a textbox.

In my opinion, reading XML this way is rather confusing. I like reading XML file using XMLDocument object better as it’s very simple and gives you a much clearer view of the whole process.

This sample project can be downloaded here.

Wednesday, May 30, 2007

Adding Attributes To An Existing XML Document

In the previous blog, I've demonstrated how to add a node or an XML element to an existing document. In this blog, I'll introduce two ways to add an attribute to an existing node.

The first way we will use the SetAttributeNode method of the DocumentElement object to add an XMLAttribute as seen below:

Dim objDocument As New XmlDocument
objDocument.LoadXml("<Cars><Make>Toyota</Make></Cars>")
Dim objAttribute1 As XmlAttribute
objAttribute1 = objDocument.CreateAttribute("Type")
objAttribute1.InnerText = "Foreign"
objDocument.DocumentElement.SetAttributeNode(objAttribute1)

The second way is to add an attribute is to call the Append method of the Attributes collection class:

Dim objAttribute2 as XmlAttribute
objAttribute2 = objDocument.CreateAttribute("Body")
objAttribute2.Value = "Sedan"
objDocument.DocumentElement.FirstChild.Attributes.Append(objAttribute2)

and finally we can show the output to the textbox with the statement:

txtOutput.Text = objDocument.InnerXml

In the end, if you run these two code blocks together, your output should look something like this:

<Cars Type="Foreign"><Make Body="Sedan">Toyota</Make></Cars>

Tuesday, May 29, 2007

Adding Nodes using InnerText Property and CreateTextNode Method

In this blog, I'll discuss two simple ways of adding a node to an existing XML document.

Before we do anything, we will need to declare an XMLDocument and call the LoadXML method which takes an XML string as the parameter to populate the document

Dim objDocument As New System.Xml.XmlDocument
objDocument.LoadXML("<Cars></Cars")

Doing this, we will obtain a document with the root node as Cars and no child element.

In the first method, we will create the element using the CreateElement method, then create an XMLText object and set the value using CreateTextNode method, add the text object to the XML element before adding it to the document as follow:

Dim objElement As System.Xml.XmlElement
Dim objText As System.Xml.XmlText
objElement = objDocument.CreateElement("Make")
objText = objDocument.CreateTextNode("Honda")
objElement.AppendChild(objText)
objDocument.DocumentElement.AppendChild(objElement)

'these last too lines produce the same result as the previous two
'objDocument.DocumentElement.AppendChild(objElement)
'objDocument.DocumentElement.LastChild.AppendChild(objText)

The second method is much simpler. We create an element using the CreateElement method of the XMLElement object, set its value in the InnerText property and add it to the document using AppendChild method:

Dim objElement As System.Xml.XmlElement
objElement = objDocument.CreateElement("Make")
objElement.InnerText = "Toyota"
objDocument.DocumentElement.AppendChild(objElement)

There you have two ways of adding a new element to an existing XML document. I prefer the second method as it is much simpler, less coding and easier to understand.

TIPS: You can quickly show the content of the XML document by sending its content to a textbox using the following statement:

txtOutput.Text = objDocument.InnerXml

Introduction to XML

For those of you just starting out with XML, I highly recommend that you check out the video tutorial(s) below:

VB

Working with XML & VB.net

C#

Working with XML & C#