Перебрать xml и добавить в gridview - PullRequest
0 голосов
/ 16 января 2012

Я пытаюсь перебрать xml и вывести узлы в сетку.

это код, который я использую:

Dim iCustomer As XPathNodeIterator = nav.Select(nav.Compile("//Content"))
While iCustomer.MoveNext() ' Loop through all customer nodes
    ' NOW you can get those child nodes
    Dim Title As XPathNodeIterator = iCustomer.Current.SelectChildren("Title", "")
    Dim iName As XPathNodeIterator = iCustomer.Current.SelectChildren("QuickLink", "")
    Dim iContact As XPathNodeIterator = iCustomer.Current.SelectChildren("Teaser", "")

    If Title.Count <> 0 Then ' If a node is found....
        ' You *might* have to call iListID.MoveNext() here
        Title.MoveNext()
        NewRow("Content_Title") = Title.Current.Value
        ' ListID = iListID.Current.Value ' ... set the value to the string
    End If
    ' Do the above for each other value
End While

Я получаю только последнийдобавлен узел, как я могу вывести все соответствующие узлы.

Ответы [ 2 ]

0 голосов
/ 16 января 2012

Попробуйте этот кусок кода:

Dim dt As New DataTable
Dim dr As DataRow
Dim iCustomer As XPathNodeIterator = nav.Select(nav.Compile("//Content"))
While iCustomer.MoveNext() ' Loop through all customer nodes
' NOW you can get those child nodes

Dim Title As XPathNodeIterator = iCustomer.Current.SelectChildren("Title", "")
Dim iName As XPathNodeIterator = iCustomer.Current.SelectChildren("QuickLink", "")
Dim iContact As XPathNodeIterator = iCustomer.Current.SelectChildren("Teaser", "")

If Title.Count <> 0 Then ' If a node is found....
    ' You *might* have to call iListID.MoveNext() here
    Title.MoveNext()
    dr = dt.NewRow() 
    dr("Content_Title") = Title.Current.Value 
    ' ListID = iListID.Current.Value ' ... set the value to the string
    dt.Rows.Add(dr)
End If
' Do the above for each other value
End While

DataGrid.DataSource = dt
0 голосов
/ 16 января 2012

Одним из самых простых способов было бы заполнить таблицу в памяти нужными строками из ваших XML-данных, а затем просто привязать ее к сетке.Я делал это много раз - очень быстро.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...