Posted by: Red Souldier | December 2, 2009

ASP.NET: Convert String XML to XML Node

For example I have string value like this:

    <?xml version="1.0"?>
           <Product type="Condom">
                <CondomName>durex</CondomName>
                <ExpDate>False</ExpDate>
                <Type>False</Type>
                <Color>False</Color>
                <Qty>False</Qty>
            </Product>

Since this this is a string value, so we need to convert to xml node to be able to process.
So this is the code:

        Imports System.Xml
        Imports System.Xml.Serialization

        Dim xmlNodeList As System.Xml.XmlNodeList
        Dim xmlNode As System.Xml.XmlNode
        Dim XmlDoc As New XmlDocument
        
	Dim _condomName as String
	Dim _ExpDate as String
	Dim _Type as String
	Dim _color as String
	Dim _qty as String

        XmlDoc.LoadXml(result)
	'Select the 1st Xml Element
        xmlNodeList = XmlDoc.GetElementsByTagName("Product")
	'Because only 1 Product Element, so select index by 0
        xmlNode = xmlNodeList.Item(0)
 
        _condomName = xmlNode.SelectSingleNode("CondomName").InnerText
        _ExpDate = xmlNode.SelectSingleNode("ExpDate").InnerText
        _Type = xmlNode.SelectSingleNode("Type").InnerText
        _color = xmlNode.SelectSingleNode("Color").InnerText
        _qty = False 'xmlNode.SelectSingleNode("Qty").InnerText

If you have more than 1 product, you need to loop the xmlList:

For Each xmlNode As XmlNode In xmlNodeList
        _condomName = xmlNode.SelectSingleNode("CondomName").InnerText
        _ExpDate = xmlNode.SelectSingleNode("ExpDate").InnerText
        _Type = xmlNode.SelectSingleNode("Type").InnerText
        _color = xmlNode.SelectSingleNode("Color").InnerText
        _qty = False 'xmlNode.SelectSingleNode("Qty").InnerText
Next
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: