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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
käµfm³d 👽Commented:
Is the field in the database of type xml or type varchar?
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.