Avatar of vcharles
vcharles
 asked on

How to add rows to xml files from several textboxes?

Hello,

I need to populate my xml files from several textboxes using VB.NET (VS 2010). Can you please guide me on how to achieve this using VB.NET.


For example  Receiver.xml contains

<Receiver_ID>1<,Receiver_ID>
<Receiver>BEL<Receiver>
<Receiver_ID>2<,Receiver_ID>
<Receiver>CAN<Receiver>
<Receiver_ID>3<,Receiver_ID>
<Receiver>FRA<Receiver>
<Receiver_ID>4<,Receiver_ID>
<Receiver>DEU<Receiver>

For example  Donor.xml contains

<Donor_ID>1<Donor_ID>
<Donor>BEL<Donor>
<Donor_ID>2<Donor_ID>
<Donor>CAN<Donor>
<Donor_ID>3<Donor_ID>

I would like to populate Reciever.xml with Textbox1 and Donor.xml with Textbox2

and when I click Save I want to populate both xml files at the same time and automaticaly increment their IDs.

Basically I will have a Form with several textboxes to populate several xml files with one click of a button but I'm using these two as an example.

Thanks,

Victor
Visual Basic.NETXML

Avatar of undefined
Last Comment
vcharles

8/22/2022 - Mon
kaufmed

What kind of text would appear in your TextBoxes?
vcharles

ASKER
Text such as BEL, CAN, USA  or DO NOT Place Item
nepaluz

I think you may have your xml files in the wrong format. Don't you want to have this for Donor.xml
<Donor ID=1>BEL</Donor>
or
<Donor>
  <ID>1</ID>
  <Name>BEL</Name>
</Donor>

and for Reciever.xml

<Receiver ID=1>BEL</Reciever>
or
<Reciever>
  <ID>1</ID>
  <Name>BEL</Name>
</Reciever>

And do you want to check for the last ID number in each file or do you just wish to add the value in the textbox?

Your help has saved me hundreds of hours of internet surfing.
fblack61
kaufmed

Agreed. There is nothing in your example that relates a Receiver to a Receiver_ID other than proximity, and XML parses don't deal in proximities--they deal in hierarchies.
vcharles

ASKER
I would like to increment the ID from the last ID in the file. Sorry. below is the proper format.
<Receiver_ID>1</Receiver_ID>
<Receiver>BEL</Receiver>
<Receiver_ID>2</Receiver_ID>
<Receiver>CAN</Receiver>
<Receiver_ID>3</Receiver_ID>
<Receiver>FRA</Receiver>
<Receiver_ID>4</Receiver_ID>
<Receiver>DEU</Receiver>

For example  Donor.xml contains

<Donor_ID>1</Donor_ID>
<Donor>BEL</Donor>
<Donor_ID>2</Donor_ID>
<Donor>CAN</Donor>
<Donor_ID>3</Donor_ID>
nepaluz

The layout of your xml files is wrong! If you are happy to carry on with the example xml files I suggested, then I can provide a code suggestion. However, with your files, it is IMPOSSIBLE!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
vcharles

ASKER
OK, please provide me the code for your xml format and I will modify my xml files.

Thanks,

Victor
nepaluz

Here goes
        Dim MyDoc As XElement = <Root>
                                    <Donor ID="3">USA</Donor>
                                    <Donor ID="1">BEL</Donor>
                                    <Donor ID="2">CAN</Donor>
                                </Root>
        Dim LargestID = (From el In MyDoc.Descendants("Donor") Select el.@<ID>).ToList
        LargestID.Sort()

        Dim CheckForItem = (From el In MyDoc.Descendants("Donor") Select el.Value).ToList
        If Not CheckForItem.Contains("Me.TextBox2.Text") Then
            Dim xNew As XElement = New XElement("Donor", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestID(LargestID.Count - 1) + 1)))
            MyDoc.Add(xNew)
        End If

Open in new window

nepaluz

There are a couple of assumptions.

1. You have to un-comment the "Me.TextBox2.Text" in lines 10 and 11 to Me.TextBox2.Text
2. You will need to load your file into an xelement, and for the example above, it is into MyDoc. Do this using for example: Dim MyDoc = XDocument.Load("C:\Donor.xml")
3. At the end of the code, you will have to save your document by calling for example: MyDoc.Save("C:\Donor.xml")
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
vcharles

ASKER
Thanks, will the code increment the ID base on the ID of the last record?
nepaluz

No the increment is based on the largest ID in the file, thats why I sort the list and get the last item!
vcharles

ASKER
Thank You, I will try it and get back to you.

Victor
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
vcharles

ASKER
By the way, I will also need to learn how to Amend and delete records for the same application, should I ask for help in a different post, or can you also send me an example on how to delete and Save changes?
Thanks,

Victor
vcharles

ASKER
Hi,
I'm getting the following error:
This operation would create an incorrectly structured document.
On Line:
MyDocReceiver.Add(xNew)
Any ideas how to fix this error?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim MyDocDonor = XDocument.Load("C:\Donor.xml")
        Dim MyDocReceiver = XDocument.Load("C:\Receiver.xml")
        Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Donor") Select el.@<ID>).ToList
        LargestIDDonor.Sort()
        Dim LargestIDReceiver = (From el In MyDocReceiver.Descendants("Receiver") Select el.@<ID>).ToList
        LargestIDReceiver.Sort()


        Dim CheckForItem = (From el In MyDocDonor.Descendants("Donor") Select el.Value).ToList
        If Not CheckForItem.Contains("Me.TextBox1.Text") Then
            Dim xNew As XElement = New XElement("Donor", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestIDDonor(LargestIDDonor.Count - 1) + 1)))
            MyDocDonor.Add(xNew)
        End If
        If Not CheckForItem.Contains("Me.TextBox2.Text") Then
            Dim xNew As XElement = New XElement("Receiver", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestIDReceiver(LargestIDReceiver.Count - 1) + 1)))
            MyDocReceiver.Add(xNew)
        End If
        MyDocDonor.Save("C:\Donor.xml")
        MyDocReceiver.Save("C:\Receiver.xml")
    End Sub

Thanks,
Victor
vcharles

ASKER
My xml files are in the following format:

Donor.xml:    
 
<?xml version="1.0" standalone="yes"?>    
<Root>    
<Row>    
<Donor_ID>1</Donor_ID>    
<Donor>BEL</Donor>    
</Row>    
<Row>    
<Donor_ID>2</Donor_ID>    
<Donor>CAN</Donor>    
</Row>    
<Row>    
<Donor_ID>3</Donor_ID>    
<Donor>CZE</Donor>    
</Row>    
<Row>    
<Donor_ID>4</Donor_ID>    
<Donor>DNK</Donor>    
</Row>    
<Row>    
<Donor_ID>5</Donor_ID>    
<Donor>FRA</Donor>    
</Row>    
<Row>    
<Donor_ID>6</Donor_ID>    
<Donor>DEU</Donor>    
</Row>    
<Row>    
</Root>    
 
 
 
Receiver.xml :    
 
<?xml version="1.0" standalone="yes"?>    
<Root>    
<Row>    
<Receiver_ID>1</Receiver_ID>    
<Receiver>BEL</Receiver>    
</Row>    
<Row>    
<Receiver_ID>2</Receiver_ID>    
<Receiver>CAN</Receiver>    
</Row>    
<Row>    
<Receiver_ID>3</Receiver_ID>    
<Receiver>CZE</Receiver>    
</Row>    
<Row>    
<Receiver_ID>4</Receiver_ID>    
<Receiver>DNK</Receiver>    
</Row>    
<Row>    
<Receiver_ID>5</Receiver_ID>    
<Receiver>FRA</Receiver>    
</Row>    
<Row>    
<Receiver_ID>6</Receiver_ID>    
<Receiver>DEU</Receiver>    
</Row>    
<Row>    
<Receiver_ID>7</Receiver_ID>    
<Receiver>GRC</Receiver>    
</Row>    
<Row>    
<Receiver_ID>8</Receiver_ID>    
<Receiver>HUN</Receiver>    
</Row>    
<Row>    
<Receiver_ID>9</Receiver_ID>    
<Receiver>ITA</Receiver>    
</Row>      
</Root  
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
nepaluz

for starters, change this

 If Not CheckForItem.Contains("Me.TextBox2.Text") Then ...

to this

 If Not CheckForItem.Contains(Me.TextBox2.Text) Then ....

then try again
nepaluz

that is for BOTH files, i.e TextBox1 and TextBox2
nepaluz

You have changed the layout of the xml, here is how to modify your code for the Donor file:
            If Not CheckForItem.Contains("Me.TextBox1.Text") Then
                Dim xNew As XElement = New XElement("Row")
                xNew.Add(New XElement("Donor_ID", CInt(LargestIDDonor(LargestIDDonor.Count - 1) + 1)))
                xNew.Add(New XElement("Donor", Me.TextBox2.Text))
                MyDocDonor.Add(xNew)
            End If

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
vcharles

ASKER
Hi,

I tried the code below, but I am getting the same error message on the same line.


Could this line be the problem?
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Donor") Select el.@<ID>).ToList

Code:
Dim MyDocDonor = XDocument.Load("C:\Donor.xml")
        Dim MyDocReceiver = XDocument.Load("C:\Receiver.xml")
        Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Donor") Select el.@<ID>).ToList
        LargestIDDonor.Sort()
        Dim LargestIDReceiver = (From el In MyDocReceiver.Descendants("Receiver") Select el.@<ID>).ToList
        LargestIDReceiver.Sort()
Dim MyDocDonor = XDocument.Load("C:\Donor.xml")
        Dim MyDocReceiver = XDocument.Load("C:\Receiver.xml")
        Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Donor") Select el.@<ID>).ToList
        LargestIDDonor.Sort()
        Dim LargestIDReceiver = (From el In MyDocReceiver.Descendants("Receiver") Select el.@<ID>).ToList
        LargestIDReceiver.Sort()


        Dim CheckForItem = (From el In MyDocDonor.Descendants("Donor") Select el.Value).ToList
        If Not CheckForItem.Contains(Me.TextBox1.Text) Then
            Dim xNew As XElement = New XElement("Row")
            xNew.Add(New XElement("Donor_ID", CInt(LargestIDDonor(LargestIDDonor.Count - 1) + 1)))
            xNew.Add(New XElement("Donor", Me.TextBox1.Text))
            MyDocDonor.Add(xNew)
        End If
        Dim CheckForItem1 = (From el In MyDocDonor.Descendants("Receiver") Select el.Value).ToList
        If Not CheckForItem1.Contains(Me.TextBox2.Text) Then
            Dim xNew As XElement = New XElement("Row")
            xNew.Add(New XElement("Receiver_ID", CInt(LargestIDDonor(LargestIDDonor.Count - 1) + 1)))
            xNew.Add(New XElement("Receiver", Me.TextBox2.Text))
            MyDocReceiver.Add(xNew)
        End If
  MyDocDonor.Save("C:\Donor.xml")
        MyDocReceiver.Save("C:\Receiver.xml")
nepaluz

you are making this harder than it ought to be. You have changed the format of the xml that I suggested but are trying to use the same code to accomplish it. Again, your xml layout is NOT good, I have changed the code below to deal with the second format that I suggested (seems yo like the nesting of the nodes!), so here goes:
            Dim MyDoc = <Root>
                            <Donor>
                                <ID>1</ID>
                                <Name>BEL</Name>
                            </Donor>
                            <Donor>
                                <ID>2</ID>
                                <Name>CAN</Name>
                            </Donor>
                            <Donor>
                                <ID>3</ID>
                                <Name>USA</Name>
                            </Donor>
                        </Root>
            Dim LargestID = (From el In MyDoc.Descendants("Donor") Select el.<ID>.Value).ToList
            LargestID.Sort()
            Dim CheckForItem = (From el In MyDoc.Descendants("Donor") Select el.Value).ToList
            If Not CheckForItem.Contains("Me.TextBox2.Text") Then
                Dim xNew As XElement = New XElement("Donor")
                xNew.Add(New XElement("ID", CInt(LargestID(LargestID.Count - 1) + 1)))
                xNew.Add(New XElement("Name", Me.TextBox2.Text.ToString.Trim))
                MyDoc.Add(xNew)
                MyDoc.Save("C:\Donor.xml") 'here is how you save
            End If

Open in new window

I saw your note above about saving and updating / amending. The saving is by calling the save method (see code) and the updating we shall deal with AFTER you settle on the xml format you are going ahead with, and better still if you ask another question all-together on that for clarity.
nepaluz

I forgot to un-comment the textbox values on line 18, change"Me.TextBox2.Text" to Me.TextBox2.Text, i.e remove the quotes!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
vcharles

ASKER
Hi,

Thank you for the code, I will try it and get back to you tomorrow.

Victor
vcharles

ASKER
Hello,
I'm not sure what I'm doing wrong but I tried your code and changed my xml file to match the one you provided but I'm still getting the following error: "This operation would create an incorrectly structured document."
on line:  MyDoc.Add(xNew)

Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim MyDoc = XDocument.Load("C:\Donor.xml")
        Dim LargestID = (From el In MyDoc.Descendants("Donor") Select el.<ID>.Value).ToList
        LargestID.Sort()
        Dim CheckForItem = (From el In MyDoc.Descendants("Donor") Select el.Value).ToList
        If Not CheckForItem.Contains(Me.TextBox2.Text) Then
            Dim xNew As XElement = New XElement("Donor")
            xNew.Add(New XElement("ID", CInt(LargestID(LargestID.Count - 1) + 1)))
            xNew.Add(New XElement("Name", Me.TextBox2.Text.ToString.Trim))
            MyDoc.Add(xNew)
            MyDoc.Save("C:\Donor.xml") 'here is how you save
        End If
        MyDoc.Save("C:\Donor.xml")
    End Sub

Donor.xmll File:

<?xml version="1.0" standalone="yes"?>
<Root>
<Donor>
<ID>1</ID>
<Name>BEL</Name>
</Donor>
<Donor>
<ID>2</ID>
<Name>CAN</Name>
</Donor>
<Donor>
<ID>3</ID>
<Name>USA</Name>
</Donor>
</Root>
vcharles

ASKER
Hi,

Can you please help me figure out what is wrong with the code in my previous post.

Thank You.

Victor
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
nepaluz

Save it to the root:
        Dim MyDoc As XDocument = XDocument.Load("C:\Donor.xml")
        Dim LargestID = (From el In MyDoc.Descendants("Donor") Select el.<ID>.Value).ToList
        LargestID.Sort()
        Dim CheckForItem = (From el In MyDoc.Descendants("Donor") Select el.Value).ToList
        If Not CheckForItem.Contains(Me.TextBox2.Text) Then
            Dim xNew As XElement = New XElement("Donor")
            xNew.Add(New XElement("ID", CInt(LargestID(LargestID.Count - 1) + 1)))
            xNew.Add(New XElement("Name", Me.TextBox2.Text.ToString.Trim))
            MyDoc.Root.Add(xNew)
        End If
        MyDoc.Save("C:\Donor.xml")

Open in new window

ASKER CERTIFIED SOLUTION
nepaluz

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
vcharles

ASKER
It Worked!
Thank You!
Victor