0

When I press the btnUpdate to show the specific elements from my XML file, it first shows the exist elements but if there is no more it's gonna show me an exception error which says "Object reference not set to an instance of an object."

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    Dim xelement As XElement = xelement.Load(cbFileName.Text)
    Dim Data As IEnumerable(Of XElement) = xelement.Elements()

    For Each dat In Data
        MsgBox(dat.Element("Name").Value & "...." & dat.Element("Tel").Value) 'Error is from this part'
    Next dat
End Sub

and here is my xml file

<Data>
  <Person>
    <Name>Alireza</Name>
    <Email>a@a.com</Email>
    <Tel>123</Tel>
  </Person>
  <Others>
    <Other_Info>This is NOOOOTTHHIIINNNG</Other_Info>
  </Others>
</Data>
EmPlusPlus
  • 171
  • 1
  • 4
  • 19

2 Answers2

3

The issue is that Name contains the child elements of the XML document you loaded - in this case, and .... (i.e., the all the name tags and their children).

What you want to do is grab a collection of all the nodes and nodes, and you can do on this way:

Dim Names As IEnumerable(Of XElement) = xelement.Descendants("Name")

For Each Name As XElement In Names 
        MsgBox((Name.Value)
Next

Same you can do for "Tel"

complete code:

 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    Dim xelement As XElement = xelement.Load(cbFileName.Text)

  Dim products As IEnumerable(Of XElement) = xelement.Descendants("Name")

    For Each product As XElement In products
        MessageBox.Show(product.Value)
    Next

    Dim Telephone As IEnumerable(Of XElement) = xelement.Descendants("Tel")

    For Each telep As XElement In Telephone
        MessageBox.Show(telep.Value)
    Next

    End Sub
Tithi Patel
  • 777
  • 4
  • 21
0

A NullReferenceException ("Object reference not set to an instance of an object.") is thrown if you call a property or method on a reference that has a Nothing value (null in C#). In your case, it is very likely that your Data collection contains elements that do not have the children that you are looking for. In order to fix this, change the code of the For Eachloop to:

For Each dat In Data
    Dim elName = dat.Element("Name")
    Dim elTel = dat.Element("Tel")
    If elName IsNot Nothing AndAlso elTel IsNot Nothing Then
        MsgBox(elName.Value & "...." & elTel.Value)
    End If
Next dat

A comprehensive overview about NullReferenceExceptions and its causes are provided in this question and its answers.

In your specific example, you seem to be interested only in the Person elements. You can also filter the descendants to receive only these elements and maybe omit the check against Nothing if you are sure that all Person elements have the required children:

Dim Data As IEnumerable(Of XElement) = xelement.Descendants("Person")
Community
  • 1
  • 1
Markus
  • 20,838
  • 4
  • 31
  • 55
  • What if I have lots of variables is there any shorter statement? – EmPlusPlus Jan 22 '14 at 09:45
  • 1
    @EmPlusPlus: the best way is to have an XML format that is not too variable. Lesser options, lesser IF statements. I've updated the answer already some time ago with the `Descendants` method that allows you to filter the `Person` elements. If you are sure that all Person elements have certain sub-elements, then you neither need the variable nor the check against Nothing for this sub-element. – Markus Jan 22 '14 at 09:52