循环遍历xml并添加到gridview

循环遍历xml并添加到gridview,xml,vb.net,gridview,Xml,Vb.net,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 = i

我试图循环遍历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
Dim iccustomer As xpathnodeiteerator=nav.Select(nav.Compile(“//Content”))
而icCustomer.MoveNext()则遍历所有客户节点
'现在您可以获取这些子节点
将标题设置为XPathNodeInterator=ICCustomer.Current.SelectChildren(“标题”,“当前”)
在名称中设置为XPathNodeInterator=ICCustomer.Current.SelectChildren(“快速链接”,“快速链接”)
Dim iContact作为XPathNodeIterator=ICCustomer.Current.SelectChildren(“摘要”和“”)
如果Title.Count为0,则“如果找到节点…”。。。。
'您*可能*必须在此处调用iListID.MoveNext()
Title.MoveNext()
NewRow(“Content_Title”)=Title.Current.Value
'ListID=iListID.Current.Value'。。。将值设置为字符串
如果结束
'对每个值执行上述操作
结束时

我只添加了最后一个节点,如何输出所有匹配的节点。

最简单的方法之一是用xml数据中的行填充内存中的表,然后将其绑定到gridview。我已经这么做过很多次了-非常快。

试试这段代码:

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
Dim dt作为新数据表
将dr设置为数据行
将iCustomer设置为XPathNodeInterator=nav.Select(nav.Compile(“//Content”))
而icCustomer.MoveNext()则遍历所有客户节点
'现在您可以获取这些子节点
将标题设置为XPathNodeInterator=ICCustomer.Current.SelectChildren(“标题”,“当前”)
在名称中设置为XPathNodeInterator=ICCustomer.Current.SelectChildren(“快速链接”,“快速链接”)
Dim iContact作为XPathNodeIterator=ICCustomer.Current.SelectChildren(“摘要”和“”)
如果Title.Count为0,则“如果找到节点…”。。。。
'您*可能*必须在此处调用iListID.MoveNext()
Title.MoveNext()
dr=dt.NewRow()
dr(“Content_Title”)=Title.Current.Value
'ListID=iListID.Current.Value'。。。将值设置为字符串
dt.行.添加(dr)
如果结束
'对每个值执行上述操作
结束时
DataGrid.DataSource=dt