Visual Basic 2008 working with XML

I have the below XML in a field in SQL 2005 and I use Visual Basic 2008 as the front end.  I would like to turn the contents of the SQL field into the text:  Dose:2 unit(s)  Frequency: As needed  Route:Nasal  Last Taken:This morning  

    <Dose>2 unit(s)</Dose>
    <Frequency>As needed</Frequency>
    <LastTaken>This morning</LastTaken>
    <Comments />

Any examples or recommendations on path to take?
Who is Participating?
Pratima PharandeCommented:
I think you need this
Sample Code

Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode

'Create the XML Document
m_xmld = New XmlDocument()

'Load the Xml file

'Show all data in your xml

'Get the list of name nodes 
m_nodelist = m_xmld.SelectNodes("/family/name")

'Loop through the nodes
For Each m_node In m_nodelist
'Get the Gender Attribute Value
Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value

'Get the firstName Element Value
Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText

'Get the lastName Element Value
Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText

'Write Result to the Console
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)

Open in new window

Umar Topia.Net Full Stack DeveloperCommented:
In FrontEnd, you can load the XML into XMlDocument and then parse the XML to add the values to the string
dastaubAuthor Commented:
can you demo the mechanics of code for that?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

käµfm³d 👽Commented:
Is the field in the database of type xml or type varchar?
dastaubAuthor Commented:
Is the field in the database of type xml or type varchar?  = xml
käµfm³d 👽Commented:
This is untested, but you should be able to do something like this:

string newText = string.Empty;

using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conn_string))
    using (System.Data.SqlClient.SqlCommand cmd =
        new System.Data.SqlClient.SqlCommand("SELECT xmlColumn FROM tableName WHERE recordKey = @keyValue", con))
        cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@keyValue", "someKey"));

            System.Xml.XmlReader reader = cmd.ExecuteXmlReader();

            newText += reader.ReadContentAsString() + " ";

            newText += reader.ReadContentAsString() + " ";

            newText += reader.ReadContentAsString() + " ";

            newText += reader.ReadContentAsString();
        catch (System.Data.SqlClient.SqlException ex)

Open in new window

This is dependent on the XML elements being listed in the order that you have displayed them above. If any nodes arrive out of order, then you will be missing data for one or more elements. This would only happen if your schema allowed arbitrary ordering of those elements, or if you are not using a schema, if your writer wrote the elements in an arbitrary order. Typical XML maintains the same ordering on its elements.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.