Please excuse my stupidity, I tend
to find the traversing
XML overly complicated.
I am using ASP.NET in VB.
I have an
XML document which contains all the details of staff in my company...
<staff>
<staffName>Test Staff</staffName>
<staffTitle>Slave</staffTitle>
<staffDepartmentName>Finance</staffDepartmentName>
<staffOffice>London</staffOffice>
<staffEmail>
[email protected]</staffEmail>
<staffPhone>0207 123 456</staffPhone>
<staffNotes>Working hours Mon
to Thurs 9.15 - 5.15</staffNotes>
<staffBio></staffBio>
</staff>
As you can see, some nodes do not always contain data for ever member of staff; only Directors have biographies.
I access the values like this...
For Each staff In ( _
From matches In myXMLFile.Descendants("staff").Descendants("staffName") _
Where matches.Nodes(0).ToString.ToLower.Contains(LCase(search)) _
Order By matches.Value _
Select matches)
staffName = staff.Descendants("staffName").Nodes(0).ToString)
staffTitle = staff.Descendants("staffTitle").Nodes(0).ToString)
staffOffice = staff.Descendants("staffOffice").Nodes(0).ToString)
staffEmail = staff.Descendants("staffEmail").Nodes(0).ToString)
staffPhone = staff.Descendants("staffPhone").Nodes(0).ToString)
staffNotes = staff.Descendants("staffNotes").Nodes(0).ToString)
staffBio = staff.Descendants("staffBio").Nodes(0).ToString)
' Do something with that data...
Next
Once it gets
to staffBio I get an error saying "Object reference not set
to an instance of an object." obviously because that node does not exist.
My question is how can I assign the value
to a variable even when it is empty without having
to do a conditional check before each assignment?