There has to be an easy answer for this, I hope!

I basically need the grandchildren of the below xml tree to show up in a vb.net gridview. I have the data getting pulled but all values are getting put into a single cell.

Here is the xml tree.

<?xml version="1.0" encoding="utf-8" ?>
<Feedmain>
  <Feed>
    <Name>ScottGu Blog</Name>
    <Url>http://weblogs.asp.net/scottgu/rss.aspx</Url>
    <testgoober>
      <goober>goober 1 - scott blog</goober>
      <goober>goober 2 - scott blog</goober>
      <goober>goober 3 - scott blog</goober>
    </testgoober>
  </Feed>
  <Feed>
    <Name>Weblogs.ASP.Net Main Feed</Name>
    <Url>http://weblogs.asp.net/MainFeed.aspx</Url>
    <testgoober>
      <goober>goober 1 - ASP.Net blog</goober>
      <goober>goober 2 - ASP.Net blog</goober>
      <goober>goober 3 - ASP.Net blog</goober>
    </testgoober>
      </Feed>
  <Feed>
    <Name>Bozo Blog</Name>
    <Url>http://somesite.com/bozo.rss</Url>
    <testgoober>
      <goober>goober 1 - Bozo blog</goober>
      <goober>goober 2 - Bozo blog</goober>
      <goober>goober 3 - Bozo blog</goober>
    </testgoober>
  </Feed>
</Feedmain>

Here is the basic code I am using. I am breaking all the data into seperate gridviews but the grandchildren just don't want to play nice. What am I missing?

BTW, sam is the one giving problems.

Dim feedXML As XDocument = XDocument.Load("c:\Feeds.xml")
        Dim bubba = From q In feedXML.Descendants("Feed") _
        Select New With { _
                    .name = q.Element("Name").Value() _
                                   }
        DataGridView3.DataSource = bubba.ToList()


        Dim yvette = From q2 In feedXML.Descendants("Feed") _
                         Select New With _
                    { _
                    .url = q2.Element("Url").Value() _
                     }
        DataGridView2.DataSource = yvette.ToList()

        'This is where we have a problem.  Right now we are getting everything as an array.  
        ' this is new, we haven't had this before.  Now how to do we break it up.
        'below is my control code.  it pulls all the values but it putting them all into a 
        'single cell.
        'Dim sam = From q3 In feedXML.Descendants("Feed") _
        '                Select New With _
        '           { _
        '           .url3 = q3.Elements("testgoober").Value() _
        '            }
        'DataGridView4.DataSource = sam.ToList()

        Dim sam = From q3 In feedXML.Descendants("Feed") _
                        Select New With _
                   { _
                   .url3 = q3.Elements("testgoober").Value() _
                    }
        DataGridView4.DataSource = sam.ToList()

When I run the code my datagrid returns the following in one cell for each set of grandchildren:

goober 1 - scott bloggoober 2 - scott bloggoober 3 - scott blog

and it need it broken up into rows like:

goober 1 - scott blog
goober 2 - scott blog
goober 3 - scott blog

Thanks for helping a newbie!
Dewayne

Recommended Answers

All 5 Replies

Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Xml.Linq
Imports System.IO
Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xmlDoc As XDocument = XDocument.Load("feeds.xml")
        Dim name As XElement
        Dim names As IEnumerable(Of XElement) = xmlDoc...<goober>
        For Each name In names
            Me.DataGridView4.Rows.Add(name.Value)
            Debug.Print(name.Value)
        Next
    End Sub
End Class

I have yet to use the query solution as it appears to compound things when you have greater than 1 of the same element. I am sure there is a way around it by passing it as a collection and iterate through it, but I have yet to try it...

HTH's
sinnerFA

That worked perfectly! Now I just have to figure out how to add in the query! Any suggestions where I would even start that process?

Thanks again for your time and help!

Dewayne

That worked perfectly! Now I just have to figure out how to add in the query! Any suggestions where I would even start that process?

Thanks again for your time and help!

Dewayne

Ask and ye shall receive....

Dim xmlDoc As XDocument = XDocument.Load("xml.xml")
        Dim sam As IEnumerable(Of XElement) = [B]From q3 In xmlDoc.Descendants("testgoober").Elements Select q3[/B]
        Dim namez As XElement
        For Each namez In sam
            Me.DataGridView1.Rows.Add(namez.Value)
            Debug.Print(namez.Value)
        Next

Perfect! Thank you so much for your help and the code!

Have a great new year!
Dewayne

Perfect! Thank you so much for your help and the code!

Have a great new year!
Dewayne

NP, You have a good one too!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.