Results 1 to 4 of 4

Thread: VB.NET: Simple XML problem....[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Simple XML problem....[Resolved]

    Say i have these XML tag:
    Code:
    <Books country = "USA" date= "21/1/2005"> 
          <Title section ="1"> 
              <Reference web = "www.any.com" topic = "1"> 
          </Title> 
          <Title section ="1"> 
              <Reference web = "www.idea.com" topic = "2"> 
          </Title> 
          <Title section ="2"> 
              <Reference web = "www.howTo.com" topic = "3"> 
          </Title> 
          <Title section ="3"> 
              <Reference web = "www.do.com" topic = "4"> 
          </Title> 
    </Books>
    how do i actually extract the country and date attributes using XMLDocument...

    I tried but it could not worked, below is my coding
    Code:
    Dim cou, da As Integer
    Dim xdoc As New XmlDocument
    Dim nodelist As XmlNodeList
    Dim node As XmlNode
    
    xdoc.Load("..\book.xml")
    
    nodelist = xdoc.SelectNodes("/Books")
    
    For Each node In nodelist
      cou = node.Attributes.GetNamedItem("country").Value
      MessageBox.Show(cou)
      da = node.Attributes.GetNamedItem("date").Value
      MessageBox.Show(da)
    Next
    Thanks
    Last edited by toytoy; Jan 21st, 2005 at 04:29 AM.

  2. #2
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Re: VB.NET: Simple XML problem

    You use the @ symbol to get an attribute, for instance.

    VB Code:
    1. Dim cou, da As Integer
    2. Dim xdoc As New XmlDocument
    3. Dim nodelist As XmlNodeList
    4. Dim node As XmlNode
    5.  
    6. xdoc.Load("..\book.xml")
    7.  
    8. nodelist = xdoc.SelectNodes("/Books")
    9.  
    10. For Each node In nodelist
    11.   cou = node.SelectSingleNode("@country").InnerText
    12.   MessageBox.Show(cou)
    13.   da = node.SelectSingleNode("@date").InnerText
    14.   MessageBox.Show(da)
    15. Next
    however I couldn't test it with your data, I kept getting some system exception which was quite strange.
    Last edited by Ideas Man; Jan 21st, 2005 at 04:45 AM.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    Re: VB.NET: Simple XML problem

    Sorry Ideas man, actually both of our code could work....

    The mistake was i forget to check the XML tag ...

    All the Reference tag should end with an proper end tag..

    Example:
    <Reference web = "www.any.com" topic = "1" />

    Thanks

  4. #4
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Re: VB.NET: Simple XML problem....[Resolved]

    Oh yeah, well spotted. I didn't see that, such a small issue caused such a large problem.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

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