Results 1 to 10 of 10

Thread: [RESOLVED] reading xml file .. description

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    Resolved [RESOLVED] reading xml file .. description

    this is the xml:

    Code:
    <TEST description="just a test">output</TEST>
    with

    Code:
    Dim _address As XmlNodeList = _doc.GetElementsByTagName("TEST")
    _address(0).InnerText
    the result will be 'output' .. how can i get the string 'just a test' from description?

    thanks in advanced

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: reading xml file .. description

    Code:
    Dim descriptions As XmlNodeList = _doc.selectNodes("/root/path/TEST/@description")
    You can then read it as

    Code:
    descriptions(0).Value

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    Re: reading xml file .. description

    works .. tnx
    Last edited by Amien; May 3rd, 2008 at 10:46 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    Re: reading xml file .. description

    another question .. can this be coded shorter

    Code:
    Dim _address As XmlNodeList = _doc.GetElementsByTagName("TEST")
    Dim _address2 as String =_address(0).InnerText
    If _address2 = "N" Then _address2 = "not found"
    If _address2 = "Y" Then _address2 = "found"
    msgbox(_address2)
    Last edited by Amien; May 3rd, 2008 at 11:01 AM.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: reading xml file .. description

    Is there a reason you're continuing to use .GetElementsByTagName despite what was mentioned in post #2?

    To answer your question, though:

    vb Code:
    1. Dim address As String = String.Empty
    2. Dim testNodes As XmlNodeList = doc.GetElementsByTagName("TEST")
    3.  
    4. If testNodes(0).InnerText = "N" Then
    5. address = "not found"
    6. Else
    7. address = "found"
    8. End If

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    Re: reading xml file .. description

    Quote Originally Posted by mendhak
    Is there a reason you're continuing to use .GetElementsByTagName despite what was mentioned in post #2?
    Yes .. cause some some lines i still use the GetElemetsByTagname, cause those lines dont have a @description added

    thanks for the info

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] reading xml file .. description

    No problem, but I suggest using the .selectNodes and .selectSingleNode method with XPath passed as arguments. I believe they are more efficient than simply getting by tag name.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    267

    Re: [RESOLVED] reading xml file .. description

    sorry .. still 1 question .. ASP.nET related

    Code:
    Dim _phonenumber As XmlNodeList =_doc.SelectNodes("/Root/PHONENUMBER")
    StatusLabel.Text = StatusLabel.Text & "<tr class='row2'><td>Phonenumber</td><td> " & _phonenumber(0).InnerText & "</td></tr>"
    some XML have a phonenumber entry .. but some dont .. if phonenumber is not found _phonenumber(0).InnerText should be empty .. but i get an error instead ..

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] reading xml file .. description

    Where do I begin...

    1. Don't concatenate HTML for your labels. Have your label simply accept a textual value, let the <tr><td> markup be ready for it around the label.

    2. If you only want one value out of some XML and don't need the rest of your node list, then use .SelectSingleNode rather than SelectNodes. There isn't a point to loading an entire node list if you need just one value.

    Code:
    Dim phoneNode As XmlNode = doc.SelectSingleNode("/Root/PHONENUMBER[1]")
    3. Check if the node is Null/Nothing before you attempt to read its .Value/.InnerText.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] reading xml file .. description

    Also, please post ASP.NET questions in the ASP.NET forum.
    http://www.vbforums.com/forumdisplay.php?f=31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width