Reading XML into an object for searching purposes

Posted on 2006-05-01
Last Modified: 2010-04-23
I would like to parse an XML file like:

<?xml version='1.0'?>
  <author firstname="William" lastname="Shakespeare">
    <book>"The Tempest"</book>
    <book>"Romeo and Juliet"</book>
    <book>"The Taming of the Shrew"</book>
  <author firstname="Steven" lastname="King">
  <author firstname="Craig" lastname="Clevenger">
    <book>"The Contortionists Handbook"</book>

into an object(array? struct? class? linked list?) and be able to use it in this way:

if user types "William" and "Shakespeare" into two text boxes then write to console books that author wrote
if no exact match then do nothing, prompt again.

Question by:donniedarko801
    LVL 4

    Expert Comment

    to start you off, and I am writing this on the fly so haven't tested anything

    Imports MSXML2.DOMDocument40Class

    Private Sub ReadXML()
    Dim XML As New MSXML2.DOMDocument40
    XML.validateOnParse = False
    Dim xAuthors As MSXML2.IXMLDOMNode
    xAuthors = XML.selectSingleNode("//Authors")
    If Not xAuthors Is Nothing Then
    Dim xbn, xin, xan, xat As MSXML2.IXMLDOMNode
    For Each xbn In xAuthors.childNodes
    s = CStr(xbn.attributes.getNamedItem("book").nodeValue)

    LVL 52

    Accepted Solution

    Heres a simple console app that should do what you want:

    Imports System.Xml

    Module Module1

        Private _doc As XmlDocument

        Sub Main()

            _doc = New XmlDocument()

            Call GetUserInput()

        End Sub

        Private Sub GetUserInput()

            Dim cont As Char = "y"
            Dim first As String
            Dim last As String

            While cont <> "n"

                Console.Write("Enter first name: ")
                first = Console.ReadLine()

                Console.Write("Enter last name: ")
                last = Console.ReadLine()

                If first = "" Or last = "" Then
                    Console.WriteLine("Please provide a first and last name!")
                    Continue While
                End If

                Dim node As XmlNode = _doc.SelectSingleNode("//author[translate(@firstname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & first.ToLower() & "' and translate(@lastname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & last.ToLower() & "']")
                If Not node Is Nothing Then
                    For Each book As XmlNode In node.ChildNodes
                    Console.WriteLine("No matches found!")
                End If

                Console.WriteLine("Search again (Y/N)?")

                cont = Console.ReadLine()(0)

            End While

        End Sub

    End Module
    LVL 1

    Author Comment

    I am looking to put them into some sort of data structue so that I can use it without parsing through the XML everytime, parsing through is something I understand, but putting it into some sort of data structure is what I need help with
    LVL 1

    Author Comment

    Sorry carl didn't read your solution correctly. Quick question about your solution:

    Dim node As XmlNode = _doc.SelectSingleNode("//author[translate(@firstname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & first.ToLower() & "' and translate(@lastname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & last.ToLower() & "']")

    how would this line look if you were not lowercasing all the text. this appears to do what I want it to, but I can assume the text will be exact.
    LVL 52

    Expert Comment

    by:Carl Tawn
    You just need to remove the translate function call, so it becomes:

    Dim node As XmlNode = _doc.SelectSingleNode("//author[@firstname='" & first.ToLower() & "' and @lastname='" & last.ToLower() & "']")
    LVL 1

    Author Comment

    Thanks Carl!

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now