使用操作数和XmlElement时出错

时间:2009-12-31 07:35:25

标签: vb.net using-statement

我的代码是这样的:

        Using StateProv As XmlElement = CType(hotelSearch.SelectSingleNode("/ota:OTA_HotelSearchRQ/ota:Criteria/ota:Criterion/ota:Address/ota:StateProv", nsmgr), XmlElement) 'i am getting error in this line....
        StateProv.SetAttribute("StateCode", BLLHotel_Search.StateCode)
        StateProv.ChildNodes(0).InnerText = BLLHotel_Search.StateName
    End Using

error:
"using operand of type 'System.xml.xmlelement' must implement 'system.idisposable'"

1 个答案:

答案 0 :(得分:1)

您无需使用使用

试试

Dim StateProv As XmlElement = CType(hotelSearch.SelectSingleNode("/ota:OTA_HotelSearchRQ/ota:Criteria/ota:Criterion/ota:Address/ota:StateProv", nsmgr), XmlElement) 'i am getting error in this line.... 
StateProv.SetAttribute("StateCode", BLLHotel_Search.StateCode) 
StateProv.ChildNodes(0).InnerText = BLLHotel_Search.StateName 

使用

的文档
  

使用关键字:

     

作为陈述,   当它在末尾定义范围时   一个物体将被处置。

检查是否有值将其与Nothing

进行比较
Dim node As XmlNode = doc.SelectSingleNode("//")
If node IsNot Nothing Then
    Dim attribute As XmlAttribute = node.Attributes(0)
End If

试试这个

Dim StateProv As XmlElement = CType(hotelSearch.SelectSingleNode("/ota:OTA_HotelSearchRQ/ota:Criteria/ota:Criterion/ota:Address/ota:StateProv", nsmgr), XmlElement) 

If StateProv IsNot Nothing Then
        StateProv.SetAttribute("StateCode", BLLHotel_Search.StateCode) 
        StateProv.ChildNodes(0).InnerText = BLLHotel_Search.StateName 
End If