Question Value of XML Node is Nothing

madala

Member
Joined
Jan 17, 2013
Messages
5
Programming Experience
10+
I have a Procedure that is supposed to get the value of an attribute in an xml file. The first 4 times I call the Procedure, it works fine. But on the 5th time the value of myNode is Nothing.

I don't think the problem is with the xml file because if change the order of the nodes, I still get the same problem - always on the 5th call of the Procedure.

Procedure:

Sub SaveMyResponse(ByVal CurrentQuestion As Integer)
Dim QuestionResponseValue As Integer
Dim i As Integer

Dim xmlDoc As New Xml.XmlDocument
xmlDoc.LoadXml(My.Resources.census_questions)

Dim myNode As XmlNode
myNode = xmlDoc.GetElementsByTagName("responses").Item(CurrentQuestion)

'Loop through responses to get the numeric value for the response
For i = 0 To myNode.ChildNodes.Count - 1
'get the value of the response
If MyComboButton.Text = myNode.ChildNodes(i).InnerText Then
surveyResponseValue = myNode.ChildNodes(i).Attributes("value").Value
Exit For
End If
Next i
End Sub


xml snippet:

<question type='dropdown' fieldname='marital' qnumber='5'>
<text>5. What is your marital status?</text>
<responses>
<response value='1'>Single</response>
<response value='2'>Married</response>
<response value='3'>Widowed</response>
<response value='4'>Divorced</response>
<response value='5'>Separated</response>
</responses>
</question>
 
I am passing it an integer. It always fails when I pass it the 6th question (CurrentQuestion = 5). Even if I chnage the order of the questions in the xml file, it still fails on question 5 as long as the question type is 'radio'.

The xml file:
VB.NET:
<?xml version='1.0' encoding='utf-8'?>
<survey name='Census Survey'>
  <description>This survey is a survey of personal information.</description>
  
  
  <question type='radio' fieldname='community' qnumber='0'>
    <text>0. Enter the SEARCH study community sdfsdf</text>
    <responses>
      <response default='false' value='1'>Rugazi</response>
      <response default='false' value='2'>Mitooma</response>
      <response default='false' value='3'>Rwashamaire</response>
      <response default='false' value='4'>Ruhoko</response>
      <response default='false' value='5'>Nsiika</response>
      <response default='false' value='6'>Bugamba</response>
      <response default='false' value='7'>Kitwe</response>
      <response default='false' value='8'>Rubaare</response>
      <response default='false' value='9'>Kazo</response>
      <response default='false' value='10'>Nyamuyanja</response>
    </responses>
    <skips></skips>
  </question>






  <question type='radio' fieldname='gender' qnumber='1'>
    <text>1. What is [xxxxxxx]'s gender?</text>
    <responses>
      <response value='1'>Male</response>
      <response value='2'>Female</response>
    </responses>
    <skips>
      <skip skipto ='headhousehold' condition='gender = "Male"'></skip>
    </skips>
  </question>




  <question type='radio' fieldname='pregnant' qnumber='2'>
    <text>2. Are you currently pregnant?</text>
    <responses>
      <response value='1'>Yes</response>
      <response value='0'>No</response>
      <response value='77'>Don't know</response>
      <response value='88'>Refuse to answer</response>
    </responses>
    <skips></skips>
  </question>
  
  
  <question type='text' fieldname='headhousehold' qnumber='3'>
    <text>3. Name of the head of the household</text>
    <defaultResponse></defaultResponse>
    <maxCharacters>1000</maxCharacters>
    <skips></skips>
  </question>
  
  
  <question type='text' fieldname='age' qnumber='4'>
    <text>4. What is [xxxxxxx]'s age?</text>
    <maxCharacters>3</maxCharacters>
    <minvalue>0</minvalue>
    <maxvalue>115</maxvalue>
    <other_values>888,999</other_values>
    <skips>
      <skip1 skipto ='6' condition='age < 14'></skip1>
    </skips>
  </question>
  
  
  <question type='radio' fieldname='marital' qnumber='5'>
    <text>5. What is [xxxxxxx]'s marital status?</text>
    <responses>
      <response value='1'>Single</response>
      <response value='2'>Married</response>
      <response value='3'>Widowed</response>
      <response value='4'>Divorced</response>
      <response value='5'>Separated</response>
      <response value='77'>Don’t know</response>
      <response value='88'>Refuse to answer</response>
    </responses>
    <skips></skips>
  </question>
  
  
  
  <question type='radio' fieldname='education' qnumber='6'>
    <text>6. What is [xxxxxxx]'s highest level of education?</text>
    <responses>
      <response value='0'>No school</response>
      <response value='1'>P1-P6</response>
      <response value='2'>P7</response>
      <response value='3'>S1-S3</response>
      <response value='4'>S4</response>
      <response value='5'>S5</response>
      <response value='6'>S6</response>
      <response value='7'>Tertiary/Vocational</response>
      <response value='8'>University</response>
      <response value='9'>Post-graduate</response>
      <response value='77'>Don’t know</response>
      <response value='88'>Refuse to answer</response>
    </responses>
    <skips></skips>
  </question>
  
  






  <question type='information' fieldname='' qnumber='7'>
    <text>7. Please press the 'Next' button to complete the survey and save the data</text>
  </question>
  <skips></skips>
  
  
</survey>
 
Last edited by a moderator:
I have added formatting tags to your code snippet. Please do so for us in future.

As for the question, have you actually counted how many <responses> elements there are in that XML? Also, I didn't ask you what type the values are that you were passing. I asked you what the values are.
 
The problem is that after the line:
myNode = xmlDoc.GetElementsByTagName("responses").Item(CurrentQuestion)
thie value of myNode is 'Nothing' if you pass CurrentQuestion = 5 and question 5 in the xml file is type 'radio' or 'dropdown'.

The problem seems to occur after calling the Procedure several times. If I move question 5 in the xml file to the top, it works fine.

Thanks in advance
 
I'm not really sure what you are asking. The first time I call the SaveMyResponse procedure, the value of CurrentQuestion = 0, the next time I call it, CurrentQuestion = 1, etc. I continue doing this, but the 6th time I call the procedure when CurrentQuestion = 5, the value of MyNode doesn't seem to get set properly - it is 'Nothing', therefore the code will fail because it is looking for the child nodes.
 
It's not rocket science. Those are the values you're using for CurrentQuestion so those are the values I wanted to know. I ask again, have you actually counted the number of <responses> elements in that XML code? There are only 5, so how are you expecting to get results for values 0 to 5? That would require that there be at least 6 <responses> elements.
 
Well thank you jmcilhinney for taking the time to look at this, I appreciate it. And I can see the mistake now. However, your tone is a bit harsh.

Cheers,
 
Back
Top