Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all

 

I have the following xml file

<?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:

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"

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted

Found a solution, changed the code for reading the xml to

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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

  • *Experts*
Posted

Since you don't mind reading the Xml all at once, try this (my VB syntax may not be right):

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("Couldn't find node")
Else
   Dim textNode As XmlNode = node.SelectSingleNode("text")
   If (textNode Is Nothing) Then
       MessageBox("Node didn't contain a ""text"" node")
   Else
       text = textNode.InnerText
   End If
End If

 

Or, if you KNOW that the node and text nodes exist:

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted (edited)

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 it's child node "text" and get it's 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:

 

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/tag's name ("data"), the attribute's name ("id") and the attribute's 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

 

here's a good link for some simple xPath commands:

http://www.w3schools.com/xpath/xpath_syntax.asp

 

Hope that helps (and isn't too late)! :-D

Edited by alreadyused

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...