0

我是 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 子句中有一个。

4

1 回答 1

1

我不熟悉 Xml 解析,但在其中两个节点中您没有该privilege属性。这会导致错误吗?

在这种情况下,如果您将子句中的Andto更改为会发生什么?这样,如果没有属性,它将不会评估语句的后半部分。AndAlsoWhereprivilege

于 2013-03-21T01:30:51.660 回答