Results 1 to 7 of 7

Thread: Xml [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Resolved Xml [Resolved]

    I'm still struggling with XML. I have this XML file with hundreds of blocks that look like this:

    <entry>
    <name></name>
    <website></website>
    <username></username>
    <password></password>
    </entry>

    How do I ready every "entry" and get the "name" and "website" into a listbox like: name ( website )
    Last edited by tylerm; Jun 19th, 2006 at 04:55 PM. Reason: Resolved

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Xml

    is there a top level node of the XML? <entry> can't be the root in a well formed XML doc, as they are only supposed to have one root element... so is it something like
    Code:
    <entries>
      <entry>
        <name></name>
        <website></website>
        <username></username>
        <password></password>
      </entry>
      <entry>
        <name></name>
        <website></website>
        <username></username>
        <password></password>
      </entry>
    </entries>

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Xml

    yes, there is a top level called <mcbr>, but yes.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Xml

    ok.. will post some example code.. give me a minute

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Xml

    ok here is some sample code..

    if you don't understand it fully, and want to see it in action, you will need to stick this code in a form, and add a listbox (listbox1)

    You would also need to put the xml document in the bin directory (where your exe is) and change my code's "testdata.xml" to whatever your XML file is actually called.

    VB Code:
    1. Dim XDoc As New Xml.XmlDocument
    2.         Try
    3.             'LOAD FILE INTO OBJECT
    4.             XDoc.Load(Application.StartupPath & "\testdata.xml")
    5.             'LOOP ALL THE CHILD NODES OF THE DOCUMENT'S DOCUMENTELEMENT PROPERTY
    6.             For Each XNode As Xml.XmlNode In XDoc.DocumentElement.ChildNodes
    7.                 ListBox1.Items.Add(XNode.Item("name").InnerText)
    8.                 ListBox1.Items.Add(ControlChars.Tab & XNode.Item("website").InnerText)
    9.                 ListBox1.Items.Add(ControlChars.Tab & XNode.Item("username").InnerText)
    10.                 ListBox1.Items.Add(ControlChars.Tab & XNode.Item("password").InnerText)
    11.             Next
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message)
    14.         End Try

  6. #6
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: Xml [Resolved]

    Hmm I post this
    vb Code:
    1. Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    2.  
    3.  
    4.         ListBox1.Items.Clear()
    5.  
    6.  
    7.  
    8.  
    9.         Dim XDoc As New Xml.XmlDocument
    10.         Try
    11.             'LOAD FILE INTO OBJECT
    12.             XDoc.Load("C:\Documents and Settings\Owner\Desktop\newslist.xml")
    13.             'LOOP ALL THE CHILD NODES OF THE DOCUMENT'S DOCUMENTELEMENT PROPERTY
    14.             For Each XNode As Xml.XmlNode In XDoc.DocumentElement.ChildNodes
    15.                 ListBox1.Items.Add(XNode.Item("rssid").InnerText)
    16.                 'ListBox1.Items.Add(ControlChars.Tab & XNode.Item("title").InnerText)
    17.                 'ListBox1.Items.Add(ControlChars.Tab & XNode.Item("link").InnerText)
    18.             Next
    19.         Catch ex As Exception
    20.             MessageBox.Show(ex.Message)
    21.         End Try
    22. End Sub

    But I get the messagebox saying "Object reference not set to an instance of an object"

    How can i fix this???

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Xml [Resolved]

    what does newslist.xml look like?

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