Team -

Please forgive my ignorance here, I am very new to xml coding. Basic situation is if I run the following code:

Dim feedXML As XDocument = XDocument.Load("c:\Feeds.xml")
        Dim bubba = From Feedmain In feedXML.Descendants("Feed") _
        Where (Feedmain.Attribute("status") Is Nothing) OrElse (Feedmain.Attribute("status").Value <> "disabled") _
        Select Name = Feedmain.Element("Name").Value, _
        Url = Feedmain.Element("otherinfo").Element("Url").Value

I can later bind the data into a gridview and get two lovely columns of text. But I don't want two columns, I want one soooooooo. I do this.

Dim feedXML As XDocument = XDocument.Load("c:\Feeds.xml")
        Dim bubba = From Feedmain In feedXML.Descendants("Feed") _
        Where (Feedmain.Attribute("status") Is Nothing) OrElse (Feedmain.Attribute("status").Value <> "disabled") _
        Select Name = Feedmain.Element("Name").Value

Then when I bind to my datagridview I get a single column but it is giving me a char count on the line of text. What am I missing?

If I have totally made myself unclear please let me know!

Could someone point me in the right direction please?

Dewayne

Dani AI

Generated

Short answer: the grid was showing the string objects' public property, not the text itself. When a DataGridView is bound to a sequence of System.String it reflects the type's public members — Length is the obvious numeric property — so you saw a single column of character counts. When your query returned an object with two named properties the grid auto-created two text columns.

Fixes that work (pick one that fits your app):

  • Return items that expose a named property (an anonymous type or a small DTO) so the grid can bind to that property.
  • Build a DataTable or use a BindingSource and add a text column manually, then map its DataPropertyName to the property you exposed.
  • Materialize the LINQ result into a concrete collection before assigning it as the DataSource so the grid inspects the actual item type.

A few cautions: always guard against missing XML elements (check for Nothing before reading Value) to avoid NullReferenceException. If you want precise control over columns, set AutoGenerateColumns = False and add columns explicitly. As demonstrated, returning objects with a named property and providing a concrete collection fixes the problem — and confirmed that approach worked.

Recommended Answers

All 2 Replies

Try this,

Dim feedXML As XDocument = XDocument.Load("c:\Feeds.xml")
        Dim bubba = From Feedmain In feedXML.Descendants("Feed") _
        Where (Feedmain.Attribute("status") Is Nothing) OrElse (Feedmain.Attribute("status").Value <> "disabled") _
        Select New With {.Name = Feedmain.Element("Name").Value()}

        DataGridView1.DataSource = bubba.ToList()

worked perfect! Thank you for the hand!

Dewayne

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.