遍历嵌套for循环VB.net中的XML

时间:2015-08-13 18:22:34

标签: xml vb.net

我正在VB.net中尝试使用XML,但我无法使嵌套的for循环工作。

以下是XML的摘录:

public void login()
{

    Query queryPK = CryptoCurrencyPUEntityManager.createNativeQuery("SELECT t.email, t.user_id FROM usertbl t");
    Query queryFK = CryptoCurrencyPUEntityManager.createNativeQuery("SELECT t.curreny, t.amount FROM CURRENCY t WHERE user_fk=?1 ");

    List<Object[]> results = queryPK.getResultList();
    for(int i = 0; i < results.size(); i++)
    {
        if(results.get(i)[0].equals(loginEmail))
        {
            int fk = (int)(results.get(i)[1]);
            System.out.println(fk);
            queryFK.setParameter(1, fk);
            List<Object[]> currencyTBLresluts = queryFK.getResultList();

            Currency cur = new Currency();


            String[] currTBLheadings = {"Currency","Amount"};

            // This is where I would put the data into the table
            // Not sure how I am meant to do that
            // jTableCurr.setModel(); maybe?

        }
    }    
}

我使用以下代码遍历组并将其输出到RichTextBox

<CustomDirectory>
  <CustomGroup>
    <Members>
      <Member>User1</Member>
      <Member>User2</Member>
      .
      .
    </Members>
  </CustomGroup>
  <Name>Some name</Name>
</CustomDirectory>

我的问题是输出在我的文件中显示了两个组的相同成员,并且这些成员属于最后一个组。我想知道我在这里做错了什么。

1 个答案:

答案 0 :(得分:0)

下面的代码将生成一个二维数组

&#13;
&#13;
Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Sub Main()
        Dim input As String = _
        "<CustomDirectory>" & _
          "<CustomGroup>" & _
            "<Members>" & _
              "<Member>User1</Member>" & _
              "<Member>User2</Member>" & _
            "</Members>" & _
          "</CustomGroup>" & _
          "<CustomGroup>" & _
            "<Members>" & _
              "<Member>User3</Member>" & _
              "<Member>User4</Member>" & _
            "</Members>" & _
          "</CustomGroup>" & _
          "<CustomGroup>" & _
            "<Members>" & _
              "<Member>User5</Member>" & _
              "<Member>User6</Member>" & _
            "</Members>" & _
          "</CustomGroup>" & _
          "<CustomGroup>" & _
            "<Members>" & _
              "<Member>User7</Member>" & _
              "<Member>User8</Member>" & _
            "</Members>" & _
          "</CustomGroup>" & _
          "<Name>Group A</Name>" & _
        "</CustomDirectory>"

        Dim cD As XElement = XElement.Parse(input)

        Dim results = cD.Descendants("CustomGroup").Select(Function(x) New With { _
              .members = x.Descendants("Member").Select(Function(y) New With {.member = CType(y, String)}).ToList()}).ToList()
    End Sub

End Module
​
&#13;
&#13;
&#13;