Results 1 to 3 of 3

Thread: VB.NET: Linking Elements and Attributes....[Resolved]

  1. #1

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

    VB.NET: Linking Elements and Attributes....[Resolved]

    Say i have this XML tag...
    Code:
    <book section=”newspaper”>
        <new id=”100”>China News</new>
        <new id=”101”>Street paper</new>
        <new id=”0”>none</it>
        …….
        <new>….</new>
      </book>
    I extract the element "China News" and "Street paper" and populate to the combobox....

    Now if i select the "China News", how do i send the id which is "100" to the

    server instead of china news...

    Thanks
    Last edited by toytoy; Jan 26th, 2005 at 02:30 PM.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: VB.NET: Linking Elements and Attributes

    Use .NET's XML classes to parse the XML and store both the Element value and attribute in a custom class which is loaded into the combobox, i.e.
    VB Code:
    1. Imports System.Xml
    2.  
    3. Public Class frmXMLExample
    4.     Inherits System.Windows.Forms.Form
    5.  
    6.     Private Structure ComboItem
    7.         Public Text As String
    8.         Public Value As Object
    9.  
    10.         Public Sub New(ByVal itemText As String, ByVal itemValue As Object)
    11.             Text = itemText
    12.             Value = itemValue
    13.         End Sub
    14.  
    15.         Public Overrides Function ToString() As String
    16.             Return Text
    17.         End Function
    18.  
    19.     End Structure
    20.  
    21. #Region " Windows Form Designer generated code "
    22.  
    23.     Public Sub New()
    24.         MyBase.New()
    25.  
    26.         'This call is required by the Windows Form Designer.
    27.         InitializeComponent()
    28.  
    29.         'Add any initialization after the InitializeComponent() call
    30.  
    31.     End Sub
    32.  
    33.     'Form overrides dispose to clean up the component list.
    34.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    35.         If disposing Then
    36.             If Not (components Is Nothing) Then
    37.                 components.Dispose()
    38.             End If
    39.         End If
    40.         MyBase.Dispose(disposing)
    41.     End Sub
    42.  
    43.     'Required by the Windows Form Designer
    44.     Private components As System.ComponentModel.IContainer
    45.  
    46.     'NOTE: The following procedure is required by the Windows Form Designer
    47.     'It can be modified using the Windows Form Designer.  
    48.     'Do not modify it using the code editor.
    49.     Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    50.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    51.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    52.         Me.ComboBox1 = New System.Windows.Forms.ComboBox
    53.         Me.TextBox1 = New System.Windows.Forms.TextBox
    54.         Me.SuspendLayout()
    55.         '
    56.         'ComboBox1
    57.         '
    58.         Me.ComboBox1.Location = New System.Drawing.Point(32, 32)
    59.         Me.ComboBox1.Name = "ComboBox1"
    60.         Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
    61.         Me.ComboBox1.TabIndex = 0
    62.         Me.ComboBox1.Text = "ComboBox1"
    63.         '
    64.         'TextBox1
    65.         '
    66.         Me.TextBox1.Location = New System.Drawing.Point(160, 32)
    67.         Me.TextBox1.Name = "TextBox1"
    68.         Me.TextBox1.TabIndex = 1
    69.         Me.TextBox1.Text = "TextBox1"
    70.         '
    71.         'frmXMLExample
    72.         '
    73.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    74.         Me.ClientSize = New System.Drawing.Size(292, 272)
    75.         Me.Controls.Add(Me.TextBox1)
    76.         Me.Controls.Add(Me.ComboBox1)
    77.         Me.Name = "frmXMLExample"
    78.         Me.Text = "frmXMLExample"
    79.         Me.ResumeLayout(False)
    80.  
    81.     End Sub
    82.  
    83. #End Region
    84.  
    85.     Private Sub frmXMLExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    86.         Dim xmlDoc As New XmlDocument
    87.         Dim nodes As XmlNodeList
    88.         Dim node As XmlNode
    89.         Dim xmlString As String
    90.  
    91.         xmlString = xmlString & "<XML>"
    92.         xmlString = xmlString & "<book section='newspaper'>"
    93.         xmlString = xmlString & "<new id='100'>China News</new>"
    94.         xmlString = xmlString & "<new id='101'>Street Paper</new>"
    95.         xmlString = xmlString & "<new id='0'>None</new>"
    96.         xmlString = xmlString & "</book>"
    97.         xmlString = xmlString & "</XML>"
    98.  
    99.         xmlDoc.LoadXml(xmlString)
    100.         nodes = xmlDoc.DocumentElement.SelectNodes("book/new")
    101.         For Each node In nodes
    102.             ComboBox1.Items.Add(New ComboItem(node.InnerText, node.Attributes.GetNamedItem("id").InnerText))
    103.         Next
    104.     End Sub
    105.  
    106.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    107.         Dim item As ComboItem = ComboBox1.SelectedItem
    108.         TextBox1.Text = item.Value
    109.     End Sub
    110. End Class

  3. #3

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

    Re: VB.NET: Linking Elements and Attributes....[Resolved]

    i try a lot of time for listbox instead of combo box but cannot work..

    what need to be change to work for listbox..

    Or is there a better approach to solve it.


    Thanks

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