Reading from XML file where tags have an id

mike55

Well-known member
Joined
Mar 26, 2004
Messages
726
Location
Ireland
Hi all

I have the following xml file
Code:
<?xml version="1.0" encoding="utf-8" ?>
<root>
  <data id="Home1">
    <text>
      <![CDATA[
      Using your web browser or mobile phone, Clubtext allows you tosend SMS <br>
      text messages to your customers, teams, staff, or club members in an easy<br>
      to use cost effective manner. 
      
      <p>Clubtext includes an administrative set of features to allow you to <br>
      manage customer/membership details and history.]]></text>
  </data>

  <data id="Home2">
    <text>
      <![CDATA[
        This is the data that belongs to home 2.  Hello world, how are you today?      
      ]]>
    </text>
  </data>
</root>

In the long term there will be a lot more information and tags in this file. What I want to do is to be able to read the data from the text tag where the id of the data tag is equal to a value that I have specified.

Here is the code that I am using to access the xml file and read from it:
Code:
Private Sub GenerateXMLMessageBody()
        Dim m_xmlr As XmlTextReader
        m_xmlr = New XmlTextReader("C:\Inetpub\wwwroot\SuretxtWebSolution\Suretxt\InformationContent.xml")
        m_xmlr.WhitespaceHandling = WhitespaceHandling.None
        m_xmlr.Read()
        m_xmlr.Read()
        Load the Loop
        While Not m_xmlr.EOF
            Go to the name tag
            m_xmlr.Read()
            if not start element exit while loop
            If Not m_xmlr.IsStartElement() Then
                Exit While
            End If
            Get the Gender Attribute Value
            Dim genderAttribute = m_xmlr.GetAttribute("id")
            Read elements firstname and lastname
            m_xmlr.Read()
            main.InnerHtml = m_xmlr.ReadElementString("text").ToString
        End While
        close the reader
        m_xmlr.Close()
    End Sub

So my question is, how can I adjust the above vb.net code so that I could in one instance specify that I want to access the text where the id of the data tag is "Home2"
 
Found a solution, changed the code for reading the xml to
Code:
Dim dsDataset As New DataSet
        dsDataset.ReadXml("C:\Inetpub\wwwroot\SuretxtWebSolution\Suretxt\InformationContent.xml")

        Dim row As Array = dsDataset.Tables(0).Select("Page=Home3")
        main.InnerHtml = row(0).itemArray(1).ToString
[code]

and added a tag <page> to identify between collections of tags.  So far the solution appears to work.

Mike55.
 
Since you dont mind reading the Xml all at once, try this (my VB syntax may not be right):
Code:
Dim xml = As New XmlDocument()
xml.Load("C:\Inetpub\wwwroot\SuretxtWebSolution\Suretxt\Infor mationContent.xml")

Dim nodeToFind As String = "Home2"
Dim text As String = String.Empty
Dim node As XmlNode = xml.SelectSingleNode("//" + nodeToFind)
If (node Is Nothing) Then
    MessageBox.Show("Couldnt find node")
Else
    Dim textNode As XmlNode = node.SelectSingleNode("text")
    If (textNode Is Nothing) Then
        MessageBox("Node didnt contain a ""text"" node")
    Else
        text = textNode.InnerText
    End If
End If

Or, if you KNOW that the node and text nodes exist:
Code:
Dim xml = As New XmlDocument()
xml.Load("C:\Inetpub\wwwroot\SuretxtWebSolution\Suretxt\Infor mationContent.xml")

Dim nodeToFind As String = "Home2"
Dim text As String = xml.SelectSingleNode("//" + nodeToFind).SelectSingleNode("text").InnerText

-ner
 
retrieve xml node based on attribute value

Mike, based on your original post, you are trying to select a parent node "data" with an attribute "id" that has a value "Home2", then select its child node "text" and get its innertext.

It looks like in an earlier post that you might have changed your xdoc? Anyway, if I understand your original needs correctly, you can do this:

Code:
dim xd as new xmldocument
dim path as string = "blahblahblah"
xd.load(path)

dim xn as xmlnode = xd.selectsinglenode("/root/data[@id=Home2]/text")
dim inText as string = xn.InnerText

The point is that since you know the node/tags name ("data"), the attributes name ("id") and the attributes value ("Home2") that is the parent to the text node you are seeking, your xPath string is:
"data[@id=Home2]"

Keep in mind that it is case sensitive; if you put "home2" your xn=nothing

heres a good link for some simple xPath commands:
http://www.w3schools.com/xpath/xpath_syntax.asp

Hope that helps (and isnt too late)! :-D
 
Last edited by a moderator:
Back
Top