svbnet
svbnet

Reputation: 1090

Getting InnerText from XmlDocument throws NullReferenceException

Whenever I try and get the InnerText of an element using an XmlDocument, it throws a NullReferenceException. Here is the code below:

    Dim SetDoc As New XmlDocument
    Dim xmlString As String = "<upload><links><bananas>apple</bananas><original>thirteen</original></links></upload>"
    SetDoc.LoadXml(xmlString)
    MsgBox(SetDoc.GetElementById("original").InnerText)

The same happens when I load exactly the same XML from file. Any ideas?

Upvotes: 0

Views: 661

Answers (1)

Jake R
Jake R

Reputation: 116

GetElementById requires an ID attribute, and also a schema defining the name of the ID attribute.

Use GetElementsByTagName instead

Msgbox(SetDoc.GetElementsByTagName("original")(0).Innertext)

(I had to put the (0), because GetElementsByTagName returns a list rather than just one item)

Or you can use the Item property

Msgbox(SetDoc.Item("upload").Item("links").Item("original").InnerText)

Upvotes: 1

Related Questions