Linq to XML检查空结果 [英] Linq to XML check for null results
本文介绍了Linq to XML检查空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果xml文件中存在数据,则以下代码有效.但是,它们不存在datafield.tag.value("90")的时间.如何检查空结果.
例子
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
推荐答案
可以使用String.IsNullOrEmpty(String)
吗?
或者,您可以使用Is Nothing进行检查,如:
Can you useString.IsNullOrEmpty(String)
?
Or, you could use Is Nothing to check, as in:
If Num90 Is Nothing Then
Num90=String.Empty
End If
为什么要为先前分配为XElement
的var
重新分配给string
,如果它为null?
如果将Num090设置为值后想要对Num090做任何事情,我将声明移到Try
块之外,并在Finally
块中进行后期处理.
Why do you want to reassign avar
that was previously assigned as aXElement
to astring
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 theTry
block, and do your post processing in theFinally
block.
这篇关于Linq to XML检查空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文