2016-07-13 30 views
0

下面的代碼給了我適合。我刪除了多餘的行李,只發布相關部分。在字段vb.net中讀取沒有值的xml文件

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 

我已經想盡辦法,我能想到的零出ProposedCustomerCharge的值時XML字段沒有價值,但IsNothing和是Nothing目前還沒有找到空字段值。

在XML文件中的字段如下所示:

<ProposedCustomerCharge></ProposedCustomerCharge> 

如何找到空場?

回答

0

這裏有幾個錯誤。第一;我沒有包含足夠的代碼。該行
Dim ql As XElement = CType(XNode.ReadFrom(xrXMLReader), XElement)
是在錯誤的地方,但你不能告訴我發佈。我最終將ql的Dim放在子的開始,並在循環內給它一個值。我從來沒有試過在循環之外使用它,但由於某種原因,它通常被設置爲無效。

這讓我在裏面如果但仍然沒有比賽。我添加了
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 />