Results 1 to 4 of 4

Thread: [RESOLVED] VB.NET 2008 and XML

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Resolved [RESOLVED] VB.NET 2008 and XML

    Hi I have an XML document that is similar to the following:

    <?xml version="1.0" encoding="utf-8"?>
    <Root>
    <Items>
    <Item ItemID="1">
    TEST1
    </Item>
    <Item ItemID="2">
    TEST2
    </Item>
    <Item ItemID="3">
    TEST3
    </Item>
    </Items>
    </Root>

    What I would like to do is have a text box and a button on a form where the user can type in say 'TEST4' and have this added to <Items> in the xml. I would also like to check if the text typed in to the text box already exists in <Items> before it is added. Is there anyway to do this?

    Kind Regards

  2. #2
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: VB.NET 2008 and XML

    Hi OP, the answer to your question is Yes and one of the objects you could use to read the xml is the XmlReader. As you would expect, there is an XmlTextWriter object that can write the new values.

    Examples online are in abundance to the degree that some have posted to leave your XmlTextWriter at home since there is already too much use of it as it is...

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: VB.NET 2008 and XML

    The following example is a partially controlled demo in that instead of a TextBox a ComboBox is used with values inline with what you asked for. If this works for you than simply replace the ComboBox for a TextBox.

    Form
    1 DataGridView with two columns created in the IDE
    Column 1 DataProperty = Identifier
    Column 2 DataProperty = Value

    1 ComboBox, 1 Button

    Feel free to adjust the new items indent level as per your liking.

    Code:
    Public Class Form1
        Private Document As New XDocument
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "Document.xml")
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.AutoGenerateColumns = False
            Document = XDocument.Load(FileName)
            DataGridView1.DataSource = _
                ( _
                    From T In Document...<Item> _
                    Select New With _
                           {   .Identifier = T.@ItemID, _
                               .Value = T.Value.Trim _
                           } _
                ).ToDataTable
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            ComboBox1.DataSource = (From T In Enumerable.Range(1, 20) Select "TEST" & T.ToString).ToList
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Result = (From T In Document...<Item> Where T.Value.Trim = ComboBox1.Text).FirstOrDefault
            If Result IsNot Nothing Then
                MessageBox.Show("Item in file already")
            Else
                Dim NextId As Integer = (From T In Document...<Item> Select CInt(T.@ItemID)).Max + 1
                Dim NewItem = <Item ItemID=<%= NextId %>><%= ComboBox1.Text %></Item>
                Document...<Items>(0).Add(NewItem)
                Document.Save(FileName)
                CType(DataGridView1.DataSource, DataTable).Rows.Add(New Object() {NextId, ComboBox1.Text})
            End If
        End Sub
    End Class
    Code Module for a language extension method (cannot be in a form)
    Code:
    <System.Diagnostics.DebuggerStepThrough()> _
    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToDataTable(Of T)(ByVal value As IEnumerable(Of T)) As DataTable
    
        Dim returnTable As New DataTable
        Dim firstRecord = value.First
    
        For Each pi In firstRecord.GetType.GetProperties
            returnTable.Columns.Add(pi.Name, pi.GetValue(firstRecord, Nothing).GetType)
        Next
    
        For Each result In value
            Dim nr = returnTable.NewRow
            For Each pi In result.GetType.GetProperties
                nr(pi.Name) = pi.GetValue(result, Nothing)
            Next
            returnTable.Rows.Add(nr)
        Next
        Return returnTable
    End Function
    XML document named Document.xml to reside in the same folder as the executable.

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <Root>
      <Items>
        <Item ItemID="1">
          TEST1
        </Item>
        <Item ItemID="2">
          TEST2
        </Item>
        <Item ItemID="3">
          TEST3
        </Item>
      </Items>
    </Root>

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2010
    Posts
    50

    Re: VB.NET 2008 and XML

    Thanks, I've managed to get this working now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width