Skip Navigation Links
Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » XML in VB.Net » datagridview to xml and vice versa
       
Author Reply
Duane
posted 6 posts
since Oct 22, 2008 
from

datagridview to xml and vice versa

  Posted on: 08 Jan 2012       
I have 4 textboxes with which I am filling a DataGridView as follows:

Column1  Column2  Column3  Column4
TextBox1.Text  TextBox2.Text  TextBox3.Text  TextBox4.Text
etc...

When I send this out to XML, I get the following:

<root>
  <Column1 xmlns: TextBox1.Text>
  <Column2>TextBox2.Text</Column2>
  <Column3>TextBox3.Text</Column3>
  <Column4>TextBox4.Text</Column4>
  </Column1>
  .
  .
  .
</root>

This is the code I'm using to generate the XML file:

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  Dim fn As New Date
  Dim tdyDate As Date = Date.Today()
  Dim setts As New XmlWriterSettings()
  setts.Indent = True
  setts.IndentChars = "  "
  Dim writer As XmlWriter = XmlWriter.Create(My.Application.Info.DirectoryPath & "\" & tdyDate.ToString("MM'-'dd'-'yyyy") & ".xml", setts)
  writer.WriteStartDocument()
  writer.WriteStartElement("Meals")
  For dr As Integer = 0 To DGV.Rows.Count - 2
  writer.WriteStartAttribute(DGV.Columns(0).HeaderText.ToString, DGV.Rows(dr).Cells(0).Value.ToString)
  writer.WriteElementString("Quantity", DGV.Rows(dr).Cells(1).Value.ToString)
  writer.WriteElementString("Description", DGV.Rows(dr).Cells(2).Value.ToString)
  writer.WriteElementString("Calories", DGV.Rows(dr).Cells(3).Value.ToString)
  writer.WriteEndElement()
  Next
  writer.WriteEndElement()
  writer.Flush()
  writer.Close()
  End Sub
 
Why am I getting the namespace attributes, instead of nodes named the same. In other words, why <Item xmlns: Name>, instead of just <Name>
Duane
Vulpes
posted  5390 posts
since  Feb 28, 2011 
from 

 Re: datagridview to xml and vice versa
  Posted on: 09 Jan 2012        0  
I take it that 'Meals' is the root element.

I'd try:

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

  Dim fn As New Date
  Dim tdyDate As Date = Date.Today()
  Dim setts As New XmlWriterSettings()
  setts.Indent = True
  setts.IndentChars = "  "
  Dim writer As XmlWriter = XmlWriter.Create(My.Application.Info.DirectoryPath & "\" & tdyDate.ToString("MM'-'dd'-'yyyy") & ".xml", setts)
  writer.WriteStartDocument()
  writer.WriteStartElement("Meals")
 
  For dr As Integer = 0 To DGV.Rows.Count - 2
  
     writer.WriteElementString(DGV.Columns(0).HeaderText.ToString, DGV.Rows(dr).Cells(0).Value.ToString)
     writer.WriteElementString("Quantity", DGV.Rows(dr).Cells(1).Value.ToString)
     writer.WriteElementString("Description", DGV.Rows(dr).Cells(2).Value.ToString)
     writer.WriteElementString("Calories", DGV.Rows(dr).Cells(3).Value.ToString)

  Next

  writer.WriteEndElement()
  writer.WriteEndDocument()
  writer.Flush()
  writer.Close()

End Sub
Duane
posted  6 posts
since  Oct 22, 2008 
from 

 Re: datagridview to xml and vice versa
  Posted on: 09 Jan 2012        0  
That is still putting the namespace tag on it. However, I think I've solved that part. I'll demonstrate my solution when I have the answer to the other part of the question, namely: how do I get it back into the Datagridview?
Duane
Duane
posted  6 posts
since  Oct 22, 2008 
from 

 Re: datagridview to xml and vice versa
  Posted on: 21 Jan 2012        0  
Is there a way to read an xml file into a datagrid without going through a dataset or datatable? I realize that this is probably the accepted procedure, but I already have a lot of working code that would end up in the round file(LOL) if I go that route. Someone must have tried this at some time in the past. This will allow me to finish a project I have been working on for over a month, which should have taken two weeks at best. Any help is appreciated.
Duane
Vulpes
posted  5390 posts
since  Feb 28, 2011 
from 

 Re: datagridview to xml and vice versa
  Posted on: 23 Jan 2012        0  
To my knowledge, you can't load the contents of an XML directly into a DGV. However, you can load them into a List of objects (as opposed to a DataSet or DataTable) and then bind the DGV to the List.

Here's an example which worked OK when I tried it just now.

The xml file, duane.xml, looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Meals>
  <Meal>
     <Quantity>quantity</Quantity>
     <Description>desc</Description>
     <Calories>calories</Calories>
  </Meal>
  <Meal>
     <Quantity>quantity2</Quantity>
     <Description>desc2</Description>
     <Calories>calories2</Calories>
  </Meal>
</Meals>

The code to load that into a DGV using LINQ to XML is as follows:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim doc As XDocument = XDocument.Load("duane.xml")
        Dim query = (From meal In doc.Descendants("Meals").Elements("Meal") Select
            Quantity = CStr(meal.Element("Quantity")),
            Description = CStr(meal.Element("Description")),
            Calories = CStr(meal.Element("Calories"))).ToList()
        DataGridView1.DataSource = query

    End Sub



       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved