PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 A simple question about XML-VBForums
Results 1 to 6 of 6

Thread: A simple question about XML

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    14

    Thumbs down A simple question about XML

    Hello my friends i am new at VB, but i have a simple question.
    I use this code to save some TextBox values to XML, but when i try to save some more, it overwrite the first one.
    What am i supposed to do in order to save more concacts?
    Code:
    Imports System
    Imports System.Xml
    Private Sub ProsthikiBTN3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProsthikiBTN3.Click
    
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
    
            ' Initialize the XmlWriter.
            Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\Βιβλίο Πρωτοκόλλου\3 - Κλοπές\MyName.xml", settings)
    
            With XmlWrt
    
                ' Write the Xml declaration.
                .WriteStartDocument()
    
                ' Write a comment.
                .WriteComment("Βιβλίο Πρωτοκόλλου")
    
                ' Write the root element.
                .WriteStartElement("TRIARI")
    
                ' Start our first person.
                .WriteStartElement("Symvan")
    
                ' The person nodes.
    
                .WriteStartElement("Protokollo")
                .WriteString(ArProtBOX3.Text.ToString())
                .WriteEndElement()
    
                .WriteStartElement("Typos")
                .WriteString(AdikimaBOX3.Text.ToString())
                .WriteEndElement()
    
                .WriteStartElement("Xronos")
                .WriteString(XronosBOX3.Text.ToString())
                .WriteEndElement()
    
                .WriteStartElement("Topos")
                .WriteString(ToposBOX3.Text.ToString())
                .WriteEndElement()
    
                .WriteStartElement("Pathontas")
                .WriteString(PathonBOX3.Text.ToString())
                .WriteEndElement()
    
                .WriteStartElement("Thlefwna")
                .WriteString(ThlefwnaBOX3.Text.ToString())
                .WriteEndElement()
    
                .WriteStartElement("AKSIP")
                .WriteString(AksipBOX3.Text.ToString())
                .WriteEndElement()
    
                ' The end of this person.
                .WriteEndElement()
    
                ' Close the XmlTextWriter.
                .WriteEndDocument()
                .Close()
    
            End With
    
            MessageBox.Show("Αποθηκεύτηκε Επιτυχώς!")
        End Sub

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

    Re: A simple question about XML

    The following example uses two TextBoxes and a button and have Option Infer On (under project properties Compile tab). As is the XML file is generated in the same folder as the executable but you could add a path to the file name also.

    Form level variables
    Code:
        Private Document As New XDocument
        Private DocName As String = "Customers.xml"
    When the form loads we check to see if the XML file exists, if it does not it is created then loaded.
    Code:
        Private Sub SomeForm_Load() Handles MyBase.Load
            If Not IO.File.Exists(DocName) Then
                Dim Header As String = "<?xml version=""1.0"" encoding=""utf-8""?>"
                Dim xmlDoc = _
                <Customers>
                </Customers>
                My.Computer.FileSystem.WriteAllText(DocName, _
                            String.Concat(Header, Environment.NewLine, xmlDoc), False)
            End If
    
            Document = XDocument.Load(DocName)
    
        End Sub

    Allow user to enter first and last name into textboxes then when pressing a button add the person to the XML file. Please note that you might like to enhance the validation which I will leave to you.

    Code:
        Private Sub AddCustomer_Click() Handles cmdAddCust.Click
            If txtFirstName.Text.Length > 0 AndAlso txtLastName.Text.Length > 0 Then
    
                Dim Checker = (From C In Document...<Customer> _
                               Where C.<FirstName>.Value = txtFirstName.Text AndAlso _
                                     C.<LastName>.Value = txtLastName.Text).FirstOrDefault
    
                If Checker Is Nothing Then
                    Dim Customer = _
                    <Customer>
                        <!--My comment-->
                        <FirstName><%= txtFirstName.Text %></FirstName>
                        <LastName><%= txtLastName.Text %></LastName>
                    </Customer>
    
                    Document.<Customers>(0).Add(Customer)
                    Document.Save(DocName)
    
                    txtFirstName.Text = ""
                    txtLastName.Text = ""
    
                    ActiveControl = txtFirstName
    
                Else
                    MsgBox("duplicate")
                End If
            End If
    
        End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    14

    Re: A simple question about XML

    Thank you very very much my friend "kevininstructor"!!!!!!
    Your solution was exact what i wanted!\
    +Rep

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

    Re: A simple question about XML

    Your welcome.

    Also for more on LINQ and XML visit Microsoft Visual Basic Developer Center for LINQ to XML Samples which has lot of good examples.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    14

    Re: A simple question about XML

    Can someone explain to me what does that code?
    vb Code:
    1. Dim Checker = (From C In Document...<Customer> _
    2.                            Where C.<FirstName>.Value = txtFirstName.Text AndAlso _
    3.                                  C.<LastName>.Value = txtLastName.Text).FirstOrDefault

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

    Re: A simple question about XML

    Quote Originally Posted by yiankos View Post
    Can someone explain to me what does that code?
    vb Code:
    1. Dim Checker = (From C In Document...<Customer> _
    2.                            Where C.<FirstName>.Value = txtFirstName.Text AndAlso _
    3.                                  C.<LastName>.Value = txtLastName.Text).FirstOrDefault
    A short explaination

    Think of this statement similar to searching a physical database table using SQL, for example you have an MS-Access table called Customer with FirstName and LastName columns and want to see if “John” “Doe” exists.


    Code:
    SELECT * FROM Customer WHERE FirstName=”John” AND LastName=”Doe”
    Keeping things simple, in the example above the statement (other than the variables John and Doe are case insensitive) the column names could be any case. When writing LINQ statements the column names are case sensitive.

    So this is the equivalent to the database SQL but against a XML document

    Code:
    Dim Checker = (From C In Document...<Customer> _                           
       Where C.<FirstName>.Value = txtFirstName.Text AndAlso _                                 
             C.<LastName>.Value = txtLastName.Text).FirstOrDefault

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width