Results 1 to 16 of 16

Thread: Append XML file or XML Alternative

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Append XML file or XML Alternative

    I am working on a program that will add or append additional data to a file or DB. I would like to use XML files, however I read elsewhere that it is not possible to append an XML file. Is this true? if so are there alternatives. If I have to I will use a DB such as Access to MYSQL but would rather not. Program will be used on several computers, I'd rather not install DB software on them if I can avoid it.

    If this can be done is there a good guide that someone can recommend?

    Thank You

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,209

    Re: Append XML file or XML Alternative

    How large will the data file be? You can always just read the existing XML into a datatable or dataset, append the new information, then save the updated XML.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    Quote Originally Posted by wes4dbt View Post
    How large will the data file be?
    It shouldn't be very big. 16 fields and perhaps 50 or 60 entries (estimated) a month (each month will have its own file).

    Do you have a good link that explains ho to work with datasets?
    Last edited by Tesla1886; Apr 14th, 2024 at 12:01 PM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Append XML file or XML Alternative

    Here's an example...

    Code:
    Dim dt As New DataTable("DataTableName")
    dt.Columns.Add("Field1Name") ' This field is String
    dt.Columns.Add("Field2Name", GetType(Integer)) ' This field is Integer
    dt.Columns.Add("Field3Name", GetType(Decimal)) ' This field is Decimal
    
    ' etc...
    
    ' this is just some random values for the three fields
    
    For x as integer = 1 to 10
        dt.Rows.Add(x.ToString, x * 5, x / (x * 5))
    Next
    Code:
    ' To save as xml...
    
    Dim fs As New IO.FileStream("C:\folder\xmlFileName.xml", IO.FileMode.OpenOrCreate)
    dt.WriteXml(fs, XmlWriteMode.WriteSchema)
    fs.Flush()
    fs.Dispose()

    Code:
    ' To read xml to a DataTable
    
    Dim dt2 As New DataTable
    dt2.ReadXml("C:\folder\xmlFileName.xml")
    Code:
    ' A DataGridView allows you to view and edit your DataTable.
    ' Any changes made in the DataGridView will be written to the DataTable
    
    ' If you don't intend to allow your user access this way, a DataTable can be edited in code, or Fields can be bound to other controls
    
    DataGridView1.DataSource = dt2

    <NewDataSet>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="DataTableName" msdata:UseCurrentLocale="true">
    <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="DataTableName">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Field1Name" type="xs:string" minOccurs="0" />
    <xs:element name="Field2Name" type="xs:int" minOccurs="0" />
    <xs:element name="Field3Name" type="xs:decimal" minOccurs="0" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    <DataTableName>
    <Field1Name>1</Field1Name>
    <Field2Name>5</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>2</Field1Name>
    <Field2Name>10</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>3</Field1Name>
    <Field2Name>15</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>4</Field1Name>
    <Field2Name>20</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>5</Field1Name>
    <Field2Name>25</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>6</Field1Name>
    <Field2Name>30</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>7</Field1Name>
    <Field2Name>35</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>8</Field1Name>
    <Field2Name>40</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>9</Field1Name>
    <Field2Name>45</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    <DataTableName>
    <Field1Name>10</Field1Name>
    <Field2Name>50</Field2Name>
    <Field3Name>0.2</Field3Name>
    </DataTableName>
    </NewDataSet>
    Last edited by .paul.; Apr 17th, 2024 at 11:17 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    Thank You .paul for the response.

    The following is what I have so far.

    Code:
        Private Sub LoadXML()
            ' Read xml to a DataTable
    
            Dim dt2 As New DataTable("ticket")
            dt2.ReadXml(FileLocation)
    
            DateAssignedMonthCalendar.DataBindings.Add("Text", dt2, "date")
            DateWorkedMonthCalendar.DataBindings.Add("Text", dt2, "worked")
    
            NameTextBox.DataBindings.Add("Text", dt2, "name")
            AddressTextBox.DataBindings.Add("Text", dt2, "address")
            CityTextBox.DataBindings.Add("Text", dt2, "city")
            ZipCodeTextBox.DataBindings.Add("Text", dt2, "zip")
            StateTextBox.DataBindings.Add("Text", dt2, "state")
    
            TicketNumberTextBox.DataBindings.Add("Text", dt2, "tnumber")
            DispatchTextBox.DataBindings.Add("Text", dt2, "dispatch")
            PhoneNumberComboBox.DataBindings.Add("Text", dt2, "pnumber")
            SerialNumberTextBox.DataBindings.Add("Text", dt2, "snumber")
    
            ReceiveComboBox.DataBindings.Add("Text", dt2, "receive")
            ReturnComboBox.DataBindings.Add("Text", dt2, "return")
    
            NotesTextBox.DataBindings.Add("Text", dt2, "notes")
    
        End Sub
    This is or will be my XML file

    Code:
    <tickets>
    	<xs:schema id="tickets" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    		<xs:element name="tickets" msdata:IsDataSet="true" msdata:MainDataTable="ticket" msdata:UseCurrentLocale="true">
    			<xs:complexType>
    				<xs:choice minOccurs="0" maxOccurs="unbounded">
    					<xs:element name="ticket">
    						<xs:complexType>
    							<xs:sequence>
    								<xs:element name="date" type="xs:string" minOccurs="0" />
    								<xs:element name="worked" type="xs:string" minOccurs="0" />
    								<xs:element name="tnumber" type="xs:string" minOccurs="0" />
    								<xs:element name="dispatch" type="xs:string" minOccurs="0" />
    								<xs:element name="snumber" type="xs:string" minOccurs="0" />
    								<xs:element name="name" type="xs:string" minOccurs="0" />
    								<xs:element name="address" type="xs:string" minOccurs="0" />
    								<xs:element name="city" type="xs:string" minOccurs="0" />
    								<xs:element name="state" type="xs:string" minOccurs="0" />
    								<xs:element name="zip" type="xs:string" minOccurs="0" />
    								<xs:element name="telephone" type="xs:string" minOccurs="0" />
    								<xs:sequence>
    									<xs:element name="pnumber" type="xs:string" minOccurs="0" />
    								</xs:sequence>								
    								<xs:element name="tracking" type="xs:string" minOccurs="0" />
    								<xs:sequence>
    									<xs:element name="receive" type="xs:string" minOccurs="0" />								
    									<xs:element name="return" type="xs:string" minOccurs="0" />
    								</xs:sequence>
    								<xs:element name="brand" type="xs:string" minOccurs="0" />
    								<xs:element name="notes" type="xs:string" minOccurs="0" />
    							</xs:sequence>
    						</xs:complexType>
    					</xs:element>
    				</xs:choice>
    			</xs:complexType>
    		</xs:element>
    	</xs:schema>
    	<ticket>
    		<date>4/1/2024</date>
    		<worked>4/1/2024</worked>
    		<tnumber>WY03240077</tnumber>
    		<dispatch>449606857</dispatch>
    		<snumber>1234ABC</snumber>
    		<name>Someone Name</name>
    		<address>123 A Street</address>
    		<city>A City</city>
    		<state>PA</state>
    		<zip>12345</zip>
    		<telephone>
    			<pnumber>01234567890</pnumber>
    		</telephone>
    		<tracking>
    			<receive>012345678912</receive>
    			<return>98765432101</return>
    		</tracking>
    		<brand>Dell</brand>
    		<notes>Fixed it</notes>
    	</ticket>
    </tickets>
    I have a couple questions

    1. With the following, ("Text", dt2, "date"), I was able to figure out what dt2 and "date" do. What does "Text" do?
    2. I have two radio buttons. How can I add / modify data in the data set depending on which radio is selected?
    3. With the way I currently have the bindings configured for the comboboxes, will everything in those comboboxes be added to the dataset. If not how can this be done
    4. If there are multiple receive, return or pnumber nodes will they all be added to the appropriate combobox? If not how can this be done?
    Last edited by Tesla1886; Apr 19th, 2024 at 09:38 PM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Append XML file or XML Alternative

    1.

    NameTextBox.DataBindings.Add("Text", dt2, "name")

    That means, bind the [current data row] “name” field from dt2 to the Text property of NameTextBox

    2.

    RadioButton1.DataBindings.Add(“Checked”, dt2, “someBooleanField”)

    3.

    You need some kind of control that you bind as I showed you with the DGV, that can change the current data row.
    Typically, you’d have something like…

    SomeComboBox.DisplayMember = “some field”
    SomeComboBox.DataSource = dt2

    The way you’re trying to bind those comboboxes won’t work

    4.

    I’m not sure of the best approach for that. If you can fix what you can with what I’ve explained, someone else might be able to suggest how to do that…

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    Quote Originally Posted by .paul. View Post
    1.

    NameTextBox.DataBindings.Add("Text", dt2, "name")

    That means, bind the [current data row] “name” field from dt2 to the Text property of NameTextBox
    I understand what the line as a whole is doing. I understand the command is binding the "name" field in the dt2 data table. What I was asking about specifically is "text". What is "Text" doing or its purpose? Given that you have
    RadioButton1.DataBindings.Add(“Checked”, dt2, “someBooleanField”)
    in #2 that "Text" is telling the binding to expect (in this case) a text string.

    Quote Originally Posted by .paul. View Post
    2.

    RadioButton1.DataBindings.Add(“Checked”, dt2, “someBooleanField”)


    The radio buttons on the form are for the user to select a brand name. RadioButton1 is "Brand A" and RadioButton2 is "Brand B". If RadioButton1 is checked I want "Brand A" written to the XML and like wise if RadoButton2 is check "Brand B" will be written to the XML.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,209

    Re: Append XML file or XML Alternative

    1. "Text" is the Text property of the TextBox

    2. "Checked" is the Checked property of the Radiobutton.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    I am having an issue adding additional data to the file.

    My code that I am currently working on is
    Code:
        Private Sub NewButton_Click(sender As Object, e As EventArgs) Handles NewButton.Click
            XMLData.Rows.Add()
    
            SaveXML()
    
        End Sub
    and

    Code:
        Private Sub SaveXML()
            Dim fs As New IO.FileStream(FileLocation, IO.FileMode.OpenOrCreate)
    
            XMLData.WriteXml(fs, XmlWriteMode.WriteSchema)
            fs.Flush()
            fs.Dispose()
    
        End Sub
    It works in a way, but not the way I want. It keeps editing the same record over and over, instead of adding data. I have searched the web but have not been able to find much about adding records, data etc. The one and only record I have in the XML I manually add to the file using a text editor for testing.

    Also will this method allow searching, editing is obvious since that is all I can do right now

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Append XML file or XML Alternative

    XMLData.Rows.Add() adds an empty new row. Assuming you had 3 fields, XMLData.Rows.Add(field1Value, field2Value, field3Value) would add a new row with data. To add an empty row, then fill in the fields later, you can use XMLData.Rows(XMLData.Rows.Count-1).Item(0) = "value"

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    Assume! Progress! Thank You

    My code is now. SaveXML() has not changed.

    Code:
            XMLData.Rows.Add(DateAssigned, DateWorked, TicketNumberTextBox.Text, DispatchTextBox.Text, SerialNumberTextBox.Text,
                                CompanyTextBox.Text, NameTextBox.Text, AddressTextBox.Text, CityTextBox.Text, StateTextBox.Text,
                                ZipCodeTextBox.Text)
    It now adds a new record and populates it with data (at least partially, still don't know what to do with comboboxes), however it not only adds a new record but modifies the the first with the same.

    Any body have any ideas why?

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,520

    Re: Append XML file or XML Alternative

    That looks like a binding error. Here's a simplified example that shows how you can use a combobox to navigate your datatable.

    DataTableToXML.zip

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    The following are my bindings (part of them)

    Code:
            DateAssignedMonthCalendar.DataBindings.Add("Text", XMLData, "date")
            DateWorkedMonthCalendar.DataBindings.Add("Text", XMLData, "worked")
    
            CompanyTextBox.DataBindings.Add("Text", XMLData, "company")
            NameTextBox.DataBindings.Add("Text", XMLData, "name")
            AddressTextBox.DataBindings.Add("Text", XMLData, "address")
            CityTextBox.DataBindings.Add("Text", XMLData, "city")
            ZipCodeTextBox.DataBindings.Add("Text", XMLData, "zip")
            StateTextBox.DataBindings.Add("Text", XMLData, "state")

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    184

    Re: Append XML file or XML Alternative

    I am using the following code to add new data to the combobox and prevent duplicates.

    Code:
            If ReceiveComboBox.Items.Count = 0 And ReceiveComboBox.Text <> "" Then
                ReceiveComboBox.Items.Add(ReceiveComboBox.Text)
    
            ElseIf ReceiveComboBox.Text <> "" Then
                For Each item As String In ReceiveComboBox.Items.ToString
                    If item <> (ReceiveComboBox.Text) Then
                        ReceiveComboBox.Items.Add(ReceiveComboBox.Text)
                        Exit For
    
                    End If
                Next
            End If
    When I run the program and attempt to add data to the combobox I get the following error

    Code:
    System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.'
    I have three comboboxs and using the same code for all three, The user will need to add data to the comboboxes to all three comboboxes.

    What can I do to resolve this?

  15. #15
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,209

    Re: Append XML file or XML Alternative

    The error message explains the problem. If you set the Datasource property, like if you bind it, then you can't use the Items.Add method to add items to the items collection.

    If you have the ComboBox bound to a field in a datatable then you would need to add the new item to the datatable and it would automatically be added to the combobox items list.

    Bind the combobox
    Code:
            Me.ComboBox1.DisplayMember = "name"
            Me.ComboBox1.ValueMember = "name"
            Me.ComboBox1.DataSource = dt
    Then to add to the items list,
    Code:
            Dim row = dt.NewRow
            row("name") = "testcb"
            dt.Rows.Add(row)
    Last edited by wes4dbt; Apr 21st, 2024 at 09:53 PM.

  16. #16
    New Member George B Gilbert's Avatar
    Join Date
    Apr 2024
    Location
    Genesee, Idaho
    Posts
    7

    Re: Append XML file or XML Alternative

    The data for my budget project is stored in normalized XML files. To add, edit, delete items in a collection of objects, the collection is de-serialized to a List(Of T). After all updates are applied to the collection, the original XML file is deleted and the updated collection is serialized to a new file.

    Code:
    Imports System.IO
    Imports System.Xml.Serialization
    Public Class C_Incomes
    
        Private moIncomes As New List(Of C_Income)
    
        Private mtIncomePath As String = String.Empty
    
        Public Sub New(ByVal IncomePath As String)
            '---------------------------------------------------------------------------------
            '     Date          Developer                      Code Change
            '  ---------- -------------------- -----------------------------------------------
            '  12/17/2014 G Gilbert            Original code
            '---------------------------------------------------------------------------------
    
            mtIncomePath = IncomePath
            If File.Exists(mtIncomePath) Then
                '** Attempt to load all existing incomes
                Dim sw As New StreamReader(mtIncomePath)
                Dim ser As New XmlSerializer(moIncomes.GetType)
                Try
                    moIncomes = ser.Deserialize(sw)
                Catch ex As Exception
                End Try
                sw.Close()
                sw.Dispose()
            End If
    
        End Sub
    Code:
            '---------------------------------------------------------------------------------
            ' Instantiate either a new income or the income being edited
            '---------------------------------------------------------------------------------
            Dim evs As New C_Incomes(gtApp_Incomes_Path)
            Dim ev As New C_Income
            Dim evIndex As Integer
            Select Case meMode
                Case EventMode.NewEvent
                    '** Nothing to do ... fall through
                Case EventMode.EditEvent
                    evIndex = evs.IndexOf(mtIncomeKey)
                    ev = evs.Item(evIndex)
            End Select
    Code:
            '---------------------------------------------------------------------------------
            ' Add/update the income to/in the collection
            '---------------------------------------------------------------------------------
            Select Case meMode
                Case EventMode.NewEvent
                    evs.Add(ev)
                Case EventMode.EditEvent
                    evs.Item(evIndex) = ev
            End Select
            evs.Save()
    Code:
        Public Sub Save()
            '---------------------------------------------------------------------------------
            ' Serialize the collection to an XML file
            '---------------------------------------------------------------------------------
            '     Date          Developer                      Code Change
            '  ---------- -------------------- -----------------------------------------------
            '  12/17/2014 G Gilbert            Original code
            '---------------------------------------------------------------------------------
    
            File.Delete(mtIncomePath)
            Dim sw As New StreamWriter(mtIncomePath)
            Dim ser As New XmlSerializer(moIncomes.GetType)
            ser.Serialize(sw, moIncomes)
            sw.Close()
            sw.Dispose()
    
        End Sub
    Last edited by George B Gilbert; Apr 24th, 2024 at 01:52 PM.

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