2012-01-16 94 views
0

我想通過一個XML循環,並把節點放入一個gridview。循環通過一個XML並添加到一個gridview

這是我使用的代碼:

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 

我只得到補充的最後一個節點,我怎麼能輸出匹配的節點。

回答

0

最簡單的方法之一是將內存中的表與您希望從xml數據獲得的行進行填充,然後將其綁定到gridview。我做了很多次 - 非常快。

0

嘗試這段代碼:

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 
相關問題