0

我正在尝试拆分字符串,但出现以下错误:

ban = Microsoft.VisualBasic.Strings.Split(xml, "<item jid='", -1,
      Microsoft.VisualBasic.CompareMethod.Binary)

这是完整的代码

Dim ban As String  
xml = xml.Replace("\\\", ")  
If (xml.IndexOf("<query xmlns='http://jabber.org/protocol/muc#admin'>") >= 0) Then  
    If (xml.IndexOf("affiliation='outcast'") >= 0) Then  
        xml = xml.Substring(xml.IndexOf("<item jid='") + 11)  
        xml = xml.Replace("' affiliation='outcast' /></query></iq>", "").Replace("' affiliation='outcast' />", "")  
        xml = xml.Replace("role='participant' />", "")  
        ban = Microsoft.VisualBasic.Strings.Split(xml, "<item jid='", -1, Microsoft.VisualBasic.CompareMethod.Binary)  
        ListBox2.Items.Clear()  
        For t = LBound(ban) To UBound(ban)  
            ListBox2.Items.Add(ban(t))  
            info.Text = "List ban Count {" + ListBox2.Items.Count + "}"  
        Next  
    End If  
End If 
4

1 回答 1

3

好的......对可用代码片段的一些研究让我明白了这一点。因此,假设 XML 文档看起来像这样:

<iq from='kinghenryv@shakespeare.lit/throne'
    id='ban1'
    to='southampton@chat.shakespeare.lit'
    type='set'>
  <query xmlns='http://jabber.org/protocol/muc#admin'>
    <item affiliation='outcast'
          jid='earlofcambridge@shakespeare.lit'/>
  </query>
</iq>

请注意,XML 可能不止于此,但它可以用于演示目的。使用字符串方法来解析 XML 是困难的方式,IMO。使用 XML API(如 LINQ to XML)要容易得多。

这是一个使用 LNQ 到 XML 的示例。

Dim xDoc = XDocument.Parse(xml)
Dim ns as XNamespace = "http://jabber.org/protocol/muc#admin"

Dim query = From x in xDoc.Descendants(ns + "item")
            Where x.Attribute("affiliation").Value = "outcast"
            Select x.Attribute("jid").Value

上面的代码将 XML 字符串加载到 XDocument 中。它还设置命名空间(包含在query元素中。

然后对 XML 进行查询。它收集所有item具有“outcast”属性的节点,affiliation然后返回相应的jid值。

然后,您可以像这样遍历集合:

For Each jid As String In query

    ListBox2.Items.Add(jid)   
Next

info.Text = "List ban Count {" + ListBox2.Items.Count.ToString() + "}"

编辑添加

如果您仍然喜欢按照代码的方式进行操作,请尝试以下更改:

Dim ban As String()

ban = Microsoft.VisualBasic.Strings.Split(xml, "<item jid='", -1, _
  Microsoft.VisualBasic.CompareMethod.Binary)

正如其他人指出的那样,Split返回一个数组,并且您已将其定义ban为 String 而不是 String() [array]。

于 2013-08-15T00:24:55.543 回答