在字段vb.net

时间:2016-07-13 15:48:05

标签: xml 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>

如何找到空白字段?

1 个答案:

答案 0 :(得分:0)

这里有几个问题。第一;我没有包含足够的代码。这条线     
Dim ql As XElement = CType(XNode.ReadFrom(xrXMLReader), XElement)
是在错误的地方,但你不知道我发布了什么。我最终将dim放在sub的开头,并在循环内给它一个值。我从来没有试过在循环外使用它,但由于某种原因,它通常被设置为空。

那让我进入了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 />

相关问题