[英]reading xml file with no value in field vb.net
以下代码适合我。 我卸下了多余的行李,只张贴了相关零件。
sQuickPath = Server.MapPath("~/App_Data/BillCalculator.xml")
Dim xrXMLReader As XmlReader = XmlReader.Create(sQuickPath)
While xrXMLReader.Read()
If xrXMLReader.NodeType = XmlNodeType.Element And xrXMLReader.Name = "ServiceType" Then
Dim ql As XElement = CType(XNode.ReadFrom(xrXMLReader), XElement)
If IsDBNull(ql.Element("ProposedCustomerCharge").Value) Then
ProposedCustomerCharge = 0.0
ElseIf IsNothing(ql.Element("ProposedCustomerCharge").Value) Then 'Check doesn't find empty element
ProposedCustomerCharge = 0.0
ElseIf ql.Element("ProposedCustomerCharge").Value Is Nothing Then
ProposedCustomerCharge = 0.0
Else
ProposedCustomerCharge = CType(ql.Element("ProposedCustomerCharge").Value, Double) 'blows chunks
End If
End If
End While
xrXMLReader.Close()
xrXMLReader = Nothing
当该xml字段没有值但IsNothing和Is Nothing没有找到空字段值时,我已经尝试过各种方法以将ProposedCustomerCharge的值清零。
xml文件中的字段如下所示:
<ProposedCustomerCharge></ProposedCustomerCharge>
如何找到空白字段?
这里有几处错误。 第一; 我没有包含足够的代码。 线
Dim ql As XElement = CType(XNode.ReadFrom(xrXMLReader), XElement)
在错误的位置,但您无法从我发布的内容中分辨出来。 我最终将Dim for ql放在子程序的开头,并在循环内为其赋值。 我从未尝试过在循环外使用它,但是由于某种原因,通常将其设置为空。
那让我进入了If,但仍然没有参加比赛。 我加了
ElseIf ql.Element("ProposedCustomerCharge").IsEmpty Then ProposedCustomerCharge = 0.0
我从中学到的是IsEmpty不会寻找空字符串。 如果该元素存在(是否为空),则返回false。 我终于发现我需要的是:
ElseIf ql.Element("ProposedCustomerCharge").Value = "" Then ProposedCustomerCharge = 0.0
实际匹配并将ProposedCustomerCharge的值设置为0.0的两倍。
我还了解到,我得到的xml没有使用最佳语法。
<ProposedCustomerCharge></ProposedCustomerCharge>
应该为<ProposedCustomerCharge />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.