Results 1 to 13 of 13

Thread: [RESOLVED] XML and VB.NET

  1. #1

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Resolved [RESOLVED] XML and VB.NET

    Hi there,

    I have an XML file, which follows...

    Code:
    <countries>
      <country>
        <name>Italy</name>
        <value>I</value>
      </country>
      <country>
        <name>Japan</name>
        <value>J</value>
      </country>
      <country>
        <name>Germany</name>
        <value>G</value>
      </country>
      <country>
        <name>USA</name>
        <value>U</value>
      </country>
    </countries>
    Question: How do I populate my Drop Down List using VB.NET? I'd like to code it in the aspx.vb file. I wish to have an output like this...

    Code:
    <asp:DropDownList ID="drpCountry" runat="server">
        <asp:ListItem Value="I">Italy</asp:ListItem>
        <asp:ListItem Value="J">Japan</asp:ListItem>
        <asp:ListItem Value="G">Germany</asp:ListItem>
        <asp:ListItem Value="U">USA</asp:ListItem>
    </asp:DropDownList>
    Thanks in advance

  2. #2
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: XML and VB.NET

    One way, using XPath (because I am old school) would be to do the following:

    Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim xmlDoc As New XmlDocument()
            xmlDoc.Load(Server.MapPath("XMLFile1.xml"))
    
            Dim countryNodes As XmlNodeList = xmlDoc.SelectNodes("/countries/country")
    
            For Each countryNode As XmlNode In countryNodes
                Dim name As XmlNode = countryNode.SelectSingleNode("name")
                Dim value As XmlNode = countryNode.SelectSingleNode("value")
    
                Dim newListItem As New ListItem(name.InnerText, value.InnerText)
                Me.DropDownList1.Items.Add(newListItem)
            Next
        End Sub
    There are other methods that you could use, for instance LINQ to XML which would get you the values that you need from the XML Nodes, but really it is a matter of preference.

    Hope that helps!!

    Gary

  4. #4

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: XML and VB.NET

    Hi there,

    Thanks Gary, that helps a lot

    Let me mark this thread as resolve and I'll post some questions later on, if I encounter some

  5. #5

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: XML and VB.NET


  6. #6

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: [RESOLVED] XML and VB.NET

    Hi there,

    Here's a question I have in mind, what are the practical uses of these kinds of XML files?

  7. #7
    Frenzied Member avrail's Avatar
    Join Date
    Mar 2006
    Location
    Egypt, Cairo
    Posts
    1,221

    Re: [RESOLVED] XML and VB.NET

    Quote Originally Posted by Claude2005 View Post
    Hi there,

    Here's a question I have in mind, what are the practical uses of these kinds of XML files?
    a lot of things,
    create menus, store informations, database what ever you want
    You Don't Have to Rate Me.
    I'm Not a Civilized Man I'm the Civilization it self
    White or Black, Living or Dieing and 0 or 1 that's MY life
    iam an Object in Object Oriented Life
    my blog : http://refateid.blogspot.com/
    twitter :@avrail
    010011000111010101110110001000000100110101111001001000000101000001100011

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] XML and VB.NET

    XML Files are typically used in a couple of specific situations. For instance,

    1) You need to have a configuration file for your application that is both easy to navigate, but also easily human readable.
    2) You need to exchange data between two systems, perhaps even across Operating System. XML format is easily understood by all systems.
    3) You need a file to store some relational data, that doesn't warrant a full blown SQL Server instance.

    But Claude, why the question, you seem to already be using XML, are you wanting to change the design?

    Gary

  9. #9

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: [RESOLVED] XML and VB.NET

    Uhm, this will be the first time I'll be working with XML in ASP.NET. The purpose of this thread is just that I just want to expand my knowledge in programming. Right now, I can only formulate solutions basing on what I currently know. Knowing more stuff means I can formulate more efficient solutions.

    are you wanting to change the design?
    I didn't understand what you meant here

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] XML and VB.NET

    Ah, fair enough, that makes sense.

    Basically, I was asking whether you are happy with the current usage of your XML file, or are you looking to change it to something else?

    Gary

  11. #11

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: [RESOLVED] XML and VB.NET

    Oh that one, it's just a simple XML to make me understand how to manipulate a XML file . Most of the tutorials I found teach you on how to manipulate the XML file but they don't show the XML file so I really can't absorb anything they teach.

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] XML and VB.NET

    Ah, I see. I thought this was a file that you were already using in your application, i.e. you had already decided to use XML. That makes more sense.

    It really comes down to how much data you have, what you are going to be doing with it, etc, all of these things will influence the decision about which data format to use.

    Gary

  13. #13

    Thread Starter
    Addicted Member Claude2005's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    166

    Re: [RESOLVED] XML and VB.NET

    Yep. I guess that sums it all up. I'll post questions here in case I encountered some.

    Thanks guys, I appreciate it

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