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  

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

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

refer
http://vbnetsample.blogspot.com/2007/07/read-xml-content-by-xmldocument.html 
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
m_xmld.Load("YourPath\test.xml")

'Show all data in your xml
MessageBox.Show(m_xmld.OuterXml)


'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)
Console.Write(vbCrLf)
Next

Open in new window

0
 
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
0
 
dastaubAuthor Commented:
can you demo the mechanics of code for that?
0
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?
0
 
dastaubAuthor Commented:
Is the field in the database of type xml or type varchar?  = xml
0
 
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"));

        try
        {
            con.Open();
            System.Xml.XmlReader reader = cmd.ExecuteXmlReader();

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

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

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

            reader.ReadToDescendant("LastTaken");
            newText += reader.ReadContentAsString();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

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.
0
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.