Results 1 to 26 of 26

Thread: Append XML file or XML Alternative

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    189

    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,212

    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
    189

    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
    189

    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
    189

    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,212

    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
    189

    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
    189

    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
    189

    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
    189

    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,212

    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.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    189

    Re: Append XML file or XML Alternative

    Quote Originally Posted by wes4dbt View Post
    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)

    Thank You.

    I have three comboboxs on my form. Two of the three will have at least one entry, the third may be empty. All three may have multiple entries.

    I have a few related questions

    1. Currently adding a second entry only adds a new row and not the second data entry. How do I deal with multiple entries for the same field in the same column?
    2. How can I prevent duplicates?
    3. Will adding the rest of the data (IE Name, address, etc) create a new row putting the phone number and the rest of the data on different rows?

  18. #18
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,212

    Re: Append XML file or XML Alternative

    1. I have no idea. Need to see your code.
    2. Here,

    Code:
            If ComboBox1.Items.Contains(ComboBox1.Text) Then
                MessageBox.Show("Is Dupicate")
            End If
    3. Add the entire row all at once.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    189

    Re: Append XML file or XML Alternative

    1. I have no other code yet to add any data to the file beyond what I have below
    2. I replaced the code I had with what you provided in your last post. I am now able to add more then one entry to the combo box, however it also allows duplicates. As currently written it also adds a new row for each entry in the combo box, which I understand how and why. What I don't understand is how to add multiple entries on the same row.
    3. I am confused here. If I have three combo boxes, each potentially having multiple entries and I need to add each entry to the table in order to have them appear in their respective combo box, I need to add the entire row for each data entry into a combo box? IE if each of the three combo boxes have one entry, I need to add the same row three times?

      Is their an easier way such as add the combo box contents to a list, then using the list to add the contents to the table?





    Code:
        Private Sub PhoneNumberComboBox_LostFocus(sender As Object, e As EventArgs) Handles PhoneNumberComboBox.LostFocus
            If Not PhoneNumberComboBox.Items.Contains(PhoneNumberComboBox.Text) And PhoneNumberComboBox.Text <> "" Then
                Dim row = XMLData.NewRow
                row("PNumber") = PhoneNumberComboBox.Text
                XMLData.Rows.Add(row)
    
            End If
    
            PhoneNumberComboBox.SelectedItem = 0
    
    
        End Sub

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    189

    Re: Append XML file or XML Alternative

    1. I have no other code yet to add any data to the file beyond what I have below
    2. I replaced the code I had with what you provided in your last post. I am now able to add more then one entry to the combo box, however it also allows duplicates. As currently written it also adds a new row for each entry in the combo box, which I understand how and why. What I don't understand is how to add multiple entries on the same row.
    3. I am confused here. If I have three combo boxes, each potentially having multiple entries and I need to add each entry to the table in order to have them appear in their respective combo box, I need to add the entire row for each data entry into a combo box? IE if each of the three combo boxes have one entry, I need to add the same row three times?

      Is their an easier way such as add the combo box contents to a list, then using the list to add the contents to the table?





    Code:
        Private Sub PhoneNumberComboBox_LostFocus(sender As Object, e As EventArgs) Handles PhoneNumberComboBox.LostFocus
            If Not PhoneNumberComboBox.Items.Contains(PhoneNumberComboBox.Text) And PhoneNumberComboBox.Text <> "" Then
                Dim row = XMLData.NewRow
                row("PNumber") = PhoneNumberComboBox.Text
                XMLData.Rows.Add(row)
    
            End If
    
            PhoneNumberComboBox.SelectedItem = 0
    
    
        End Sub

  21. #21
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,212

    Re: Append XML file or XML Alternative

    I don't understand what your trying to do.

    You sound like you want to add multiple phone#'s and multiple addresses etc on one row of XMLData. I thought each row of XMLData would have one phone# and one address etc. So I'm not sure what your doing.

    For some reason when the combobox is bound the "Contains" method works differently. Don't remember why. But you can check like this,

    Code:
            Dim num() As DataRow = dt.Select("Name='" & ComboBox1.Text & "'")
            If num.Length = 0 Then
                'add row
                MessageBox.Show("Not dup")
            Else
                MessageBox.Show("Is Duplicate")
            End If

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    189

    Re: Append XML file or XML Alternative

    Quote Originally Posted by wes4dbt View Post
    I don't understand what your trying to do.

    You sound like you want to add multiple phone#'s and multiple addresses etc on one row of XMLData. I thought each row of XMLData would have one phone# and one address etc. So I'm not sure what your doing.
    I will have multiple phone numbers (IE cell phone and / or office phone and / or secondary contact) but one address, the other combo boxes will be receive and return tracking numbers. In typical database lingo the address will be one
    while phone numbers and receive / return tracking numbers will be many.

  23. #23
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,212

    Re: Append XML file or XML Alternative

    This really sounds like something a relational database wound be the best solution. If it must be XML there is probably a way but I don't know it.

    Having ONE record in XML with unlimited phone#'s and tracking numbers is something I've never done. Thought I remember seeing an article about using XML as a relational database.

    Good luck

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    189

    Re: Append XML file or XML Alternative

    Quote Originally Posted by wes4dbt View Post
    This really sounds like something a relational database wound be the best solution. If it must be XML there is probably a way but I don't know it.

    Having ONE record in XML with unlimited phone#'s and tracking numbers is something I've never done. Thought I remember seeing an article about using XML as a relational database.

    Good luck
    I'd rather not use a relational DB as I'd rather not have to install Access, MYSQL or whichever DB.

    I wrote a program to create and edit XML files to import my DVDs into PVR software. I was able to add the cast (including guest actors), the crew (writers, directors, producers (including Executive and associate).
    the XML had something along the lines of:
    Code:
          <Cast>
            <Member>Actor:Carroll O'Connor</Member>
            <Member>Actor:Jean Stapleton</Member>
            <Member>Actor:Rob Reiner</Member>
            <Member>Actor:Sally Struthers</Member>
            <Member>Actor:Mike Evans</Member>
          </Cast>
    Thus I was hoping to have something along the lines of
    Code:
    <Phone>
            <Number>1234567890</Number>
            <Number>0987654321</Number>
    </Phone>
    I used a different method in that program and tried it in this one, but it wasn't working out as well. Maybe I'll take a look again. I only know what I learned when I took a basic course in college (a 100 level course at that). I got assistance from the community on that one too.

    The forum is here

    Thank You anyway.

  25. #25
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,776

    Re: Append XML file or XML Alternative

    If you are creating this why are you using namespaces?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  26. #26
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,212

    Re: Append XML file or XML Alternative

    Had some free time and I was curious about your approach. Here is a simple proof of concept example of a relational DataSet using XML. If you want to check it out.

    First create the XML file,

    Code:
    Public Class CreateXMLFileWithRelations
        Private ds As New DataSet
        Private dtNames As New DataTable("Names")
        Private dtTrackNo As New DataTable("TrackNo")
        Private dtPhoneNo As New DataTable("PhoneNo")
    
        Private Sub CreateXMLFileWithRelations_Load(sender As Object, e As EventArgs) Handles Me.Load
            SetUpData()
            SetUpRelations()
        End Sub
        Private Sub SetUpData()
    
            dtNames.Columns.Add("ID", GetType(Integer))
            dtNames.Columns.Add("Name", GetType(String))
    
    
            dtNames.Rows.Add(1, "Movie1")
            dtNames.Rows.Add(2, "SomeMovie")
            dtNames.Rows.Add(3, "DumbMovie")
    
            dtTrackNo.Columns.Add("NameID", GetType(Integer))
            dtTrackNo.Columns.Add("TrackNo", GetType(String))
    
            dtTrackNo.Rows.Add(3, "abc123")
            dtTrackNo.Rows.Add(3, "def123")
            dtTrackNo.Rows.Add(1, "ghi333")
            dtTrackNo.Rows.Add(1, "www123")
    
            dtPhoneNo.Columns.Add("NameID", GetType(Integer))
            dtPhoneNo.Columns.Add("PhoneNo", GetType(String))
    
            dtPhoneNo.Rows.Add(1, "1234567890")
            dtPhoneNo.Rows.Add(2, "1111111111")
    
            ds.Tables.Add(dtNames)
            ds.Tables.Add(dtTrackNo)
            ds.Tables.Add(dtPhoneNo)
    
    
        End Sub
    
        Private Sub SetUpRelations()
    
            ds.Relations.Add("NameTrackNo", ds.Tables("Names").Columns(0), ds.Tables("TrackNo").Columns(0))
            ds.Relations.Add("NamePhoneNo", ds.Tables("Names").Columns(0), ds.Tables("PhoneNo").Columns(0))
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ds.WriteXml("TestFile.xml", XmlWriteMode.WriteSchema)
        End Sub
    End Class
    Then add another form with this code,

    Code:
    Imports System.ComponentModel
    Imports System.Windows.Forms.VisualStyles.VisualStyleElement
    
    Public Class Form3
        Private ds As New DataSet
        'Private dtNames As New DataTable("Names")
        'Private dtTrackNo As New DataTable("TrackNo")
        'Private dtPhoneNo As New DataTable("PhoneNo")
        Private NamesBindingSource As New BindingSource
        Private NameTrackNoBindingsource As New BindingSource
        Private PhoneNoBindingsource As New BindingSource
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            ds.ReadXml("TestFile.xml")
            
            ds.AcceptChanges()
    
            NamesBindingSource.DataSource = ds
            NamesBindingSource.DataMember = "Names"
    
            NameTextBox.DataBindings.Add("Text", NamesBindingSource, "Name")
    
            NameTrackNoBindingsource.DataSource = NamesBindingSource
            NameTrackNoBindingsource.DataMember = "NameTrackNo"
            TrackNoComboBox.DisplayMember = "Trackno"
            TrackNoComboBox.DataSource = NameTrackNoBindingsource
    
            PhoneNoBindingsource.DataSource = NamesBindingSource
            PhoneNoBindingsource.DataMember = "NamePhoneNo"
            PhoneNoComboBox.DisplayMember = "PhoneNo"
            PhoneNoComboBox.DataSource = PhoneNoBindingsource
    
            DataGridView1.DataSource = NamesBindingSource
    
        End Sub
    
    
        Private Sub SaveChanges_Click(sender As Object, e As EventArgs) Handles SaveChangesButton.Click
    
            ds.WriteXml("TestFile.xml", XmlWriteMode.WriteSchema)
            ds.AcceptChanges()
        End Sub
    
        Private Sub TrackNoComboBox_Leave(sender As Object, e As EventArgs) Handles TrackNoComboBox.Leave
            Dim num() As DataRow = ds.Tables("TrackNo").Select("TrackNo='" & TrackNoComboBox.Text & "'")
            If num.Length = 0 AndAlso MessageBox.Show("Add New Tracking Number?", "Save Change", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                'add row
                Dim row = ds.Tables("TrackNo").NewRow
                row("NameID") = CInt(DataGridView1.CurrentRow.Cells(0).Value)
                row("TrackNo") = TrackNoComboBox.Text
                ds.Tables("TrackNo").Rows.Add(row)
                TrackNoComboBox.SelectedIndex = (TrackNoComboBox.Items.Count - 1)
            End If
        End Sub
    
        Private Sub PhoneNoComboBox_Leave(sender As Object, e As EventArgs) Handles PhoneNoComboBox.Leave
            Dim num() As DataRow = ds.Tables("PhoneNo").Select("PhoneNo='" & PhoneNoComboBox.Text & "'")
            If num.Length = 0 AndAlso MessageBox.Show("Add New Phone Number?", "Save Change", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                'add row
                Dim row = ds.Tables("PhoneNo").NewRow
                row("NameID") = CInt(DataGridView1.CurrentRow.Cells(0).Value)
                row("PhoneNo") = PhoneNoComboBox.Text
                ds.Tables("PhoneNo").Rows.Add(row)
                PhoneNoComboBox.SelectedIndex = (PhoneNoComboBox.Items.Count - 1)
            End If
        End Sub
    
        Private Sub Form3_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
            If ds.HasChanges Then
                If MessageBox.Show("Data Has Been Modified.  Save Changes?", "Save", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                    ds.WriteXml("TestFile.xml", XmlWriteMode.WriteSchema)
                End If
            End If
        End Sub
    
        Private Sub DataGridView1_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
            Dim num As Integer = ds.Tables("Names").Compute("Max([" & "ID" & "])", "")
            e.Row.Cells(0).Value = num + 1
    
        End Sub
    
    End Class
    Add control to the form

    NamesTextBox
    TrackNoComboBox
    PhoneNoComboBox
    DataGridView1
    SaveChangesButton

    I should add this example doesn't address how you would Edit/Delete the TrackNo or PhoneNo items. It only adds items.
    Last edited by wes4dbt; Today at 03:38 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