Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code works if data exist in the xml file. However, their times when datafield.tag.value(“90”) doesn’t exist. How can I check for null results.
example
if Num090 = Null then
Num090 = empty string


Appreciate your help.

Dim xd As XElement = XElement.Load(strFilename)
Dim strPipe As String = Chr(124)
Dim strSpace As String = Chr(10)
Dim strFinalString As String = ""
Dim Records = (From datafields In xd.Descendants("record") Select datafields)
For Each ser As XElement In Records
     Try
           Dim Num090 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "090")
           Select datafield.Elements.ElementAt(0))

           Dim DTIC856 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "856")
           Select datafield).Skip(1).First()
              

     Catch ex As Exception

     Finally

     End Try

Next
Posted
Updated 6-May-11 6:06am
v2

Can you use String.IsNullOrEmpty(String)?

EDIT: Or, you could use Is Nothing to check, as in:
If Num90 Is Nothing Then
  Num90=String.Empty
End If
 
Share this answer
 
v2
Comments
stopete82 6-May-11 12:53pm    
Thanks for the response but that won't worked because the fallowing code if their is now value for Num090 I get a exception error. This variable doesn't return an string.

Dim Num090 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "090")
Select datafield.Elements.ElementAt(0))
Marc A. Brown 6-May-11 12:56pm    
Ok, how about an Is Nothing check? If Num90 Is Nothing Then ... I'll also add this to the answer.
stopete82 6-May-11 14:16pm    
yes that's what I am having problems. I would like to check if Num090 is null first. How can I dot that? Thanks again.
Marc A. Brown 6-May-11 14:19pm    
Check my answer again. I added Is Nothing code there.
stopete82 6-May-11 14:39pm    
Sorry I missed that but that worked.

Code that worked:

Dim Num090 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "090")
Select datafield.Elements.ElementAt(0))

If IsNothing(Num090) Then

Num090.Value = String.Empty

MsgBox(Num090.Value)

Else

MsgBox(Num090.Value)

End If

Thanks
Why do you want to reassign a var that was previously assigned as a XElement to a string if it's null?

If there's anything you want to do to Num090 after setting it to a value, I'd move the declaration outside the Try block, and do your post processing in the Finally block.
 
Share this answer
 
Comments
stopete82 6-May-11 13:01pm    
For the fallowing code I want to capture the value for Num090 but sometimes it won't have a value.

with value xml file:
<datafield tag="090" ind1="1" ind2="0">
<subfield code="a">Control over the Nile

No value xml :
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">

or sometimes it won't exist:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900