Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

What kind of text would appear in your TextBoxes?
Avatar of Victor  Charles

ASKER

Text such as BEL, CAN, USA  or DO NOT Place Item
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?

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.
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>
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!
OK, please provide me the code for your xml format and I will modify my xml files.

Thanks,

Victor
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

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")
Thanks, will the code increment the ID base on the ID of the last record?
No the increment is based on the largest ID in the file, thats why I sort the list and get the last item!
Thank You, I will try it and get back to you.

Victor
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
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
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  
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
that is for BOTH files, i.e TextBox1 and TextBox2
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

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")
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.
I forgot to un-comment the textbox values on line 18, change"Me.TextBox2.Text" to Me.TextBox2.Text, i.e remove the quotes!
Hi,

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

Victor
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>
Hi,

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

Thank You.

Victor
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
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It Worked!
Thank You!
Victor