Retrieve content data in xml childnodes
By bryan tugade
This is to show you on how to retrieve content data in the childnodes of xml and show it in web page using response.write.
This is the sample xml
filename : myxml.xml
<?xml version="1.0" encoding="utf-8" ?>
<products>
<product>
<imageurl>http://static.eggheadcafe.com/images/EggHeadCafeLogoTransparent.png</imageurl>
<modelName>This is logo</modelName>
<type>snippets</type>
<description>
<![CDATA[ <p> Logo : the logo</p> <p> category : Information Technology</p> <table class="style1"> <tr> <td class="style2"> This is a table</td> <td> </td> </tr> </table>
]]>
</description>
</product>
</products>
Here's the code in our code behind
Imports System.Xml
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
getchildnodeData()
End Sub
Private Sub getchildnodeData()
Dim xmlFilename As String = Server.MapPath(".") & "\app_data\myxml.xml"
Dim xmlDoc As New XmlDocument()
Dim arr As New ArrayList
If File.Exists(xmlFilename) Then
xmlDoc.Load(xmlFilename)
Dim elmnt As XmlElement = xmlDoc.DocumentElement
Dim lstproducts As XmlNodeList = elmnt.ChildNodes
For Each node As XmlNode In lstproducts
For i As Integer = 0 To node.ChildNodes.Count - 1
Response.Write(node.ChildNodes.Item(i).InnerText & "<br/>")
Next
Next
End If
End Sub
End Class
If we run this , the result would be like this
The output would be in just one line, I just add a <br> tag to create a new line for each childnodes like this one
Response.Write(node.ChildNodes.Item(i).InnerText & "<br/>")
That's it!, Happy coding
Related FAQs
What if we use xml as our database? And sometimes we also want to save the data as html or the whole html source code. We can do this by using character data or simply CDATA. The format for this is "" . Here's a simple snippets on how to add it in xml.
Retrieve content data in xml childnodes (1295 Views)