Duane

Duane

  • NA
  • 77
  • 0

datagridview to xml and vice versa

Jan 8 2012 4:35 PM
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>

Answers (4)