我是 LINQ 的新手,所以我认为我的语法有一些错误。我在网上看了很多例子,但每个人似乎都有自己的风格。
这是我要查询的 XML 数据:
<ajax-response>
<response type="object" id="unknown">
<generic response="Success" message="Channel status will follow" />
</response>
<response type="object" id="unknown">
<generic event="Status" privilege="Call" channel="SIP/452-000006fc" calleridnum="452" calleridname="Joe" connectedlinenum="430" connectedlinename="device" accountcode="" channelstate="6" channelstatedesc="Up" context="macro-dial-one" extension="s" priority="37" seconds="54" bridgedchannel="SIP/430-000006fd" bridgeduniqueid="1363822334.1829" uniqueid="1363822334.1828" />
</response>
<response type="object" id="unknown">
<generic event="Status" privilege="Call" channel="SIP/430-000006fd" calleridnum="430" calleridname="device" connectedlinenum="452" connectedlinename="Joe" account="" state="Up" bridgedchannel="SIP/452-000006fc" bridgeduniqueid="1363822334.1828" uniqueid="1363822334.1829" />
</response>
<response type="object" id="unknown">
<generic event="StatusComplete" items="2" />
</response>
</ajax-response>
这是我到目前为止所拥有的:
Function ParseXML(statusXML As XmlDocument) As String
Dim xdoc As XDocument = XDocument.Load(New XmlNodeReader(statusXML))
Dim parsed As StringBuilder = New StringBuilder()
Dim query = From generic In xdoc.Descendants("generic") _
Where generic.Attribute("privilege") IsNot Nothing And generic.Attribute("privilege").Value = "Call" _
Select connectedlinenum = generic.Attribute("connectedlinenum").ToString, _
calleridnum = generic.Attribute("calleridnum").ToString
For Each i In query
parsed.Append(i.connectedlinenum).Append(",").Append(i.calleridnum).Append(vbCrLf)
Next
Return parsed.ToString
End Function
我猜我误解了我的generic
对象应该如何工作,因为NullReferenceException
当我尝试引用它时,我的 Where 子句中有一个。