Victor Charles
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_I D>
<Receiver>BEL<Receiver>
<Receiver_ID>2<,Receiver_I D>
<Receiver>CAN<Receiver>
<Receiver_ID>3<,Receiver_I D>
<Receiver>FRA<Receiver>
<Receiver_ID>4<,Receiver_I D>
<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
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_I
<Receiver>BEL<Receiver>
<Receiver_ID>2<,Receiver_I
<Receiver>CAN<Receiver>
<Receiver_ID>3<,Receiver_I
<Receiver>FRA<Receiver>
<Receiver_ID>4<,Receiver_I
<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
What kind of text would appear in your TextBoxes?
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?
<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.
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_I D>
<Receiver>BEL</Receiver>
<Receiver_ID>2</Receiver_I D>
<Receiver>CAN</Receiver>
<Receiver_ID>3</Receiver_I D>
<Receiver>FRA</Receiver>
<Receiver_ID>4</Receiver_I D>
<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>
<Receiver_ID>1</Receiver_I
<Receiver>BEL</Receiver>
<Receiver_ID>2</Receiver_I
<Receiver>CAN</Receiver>
<Receiver_ID>3</Receiver_I
<Receiver>FRA</Receiver>
<Receiver_ID>4</Receiver_I
<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!
ASKER
OK, please provide me the code for your xml format and I will modify my xml files.
Thanks,
Victor
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
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.x ml")
3. At the end of the code, you will have to save your document by calling for example: MyDoc.Save("C:\Donor.xml")
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.x
3. At the end of the code, you will have to save your document by calling for example: MyDoc.Save("C:\Donor.xml")
ASKER
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!
ASKER
Thank You, I will try it and get back to you.
Victor
Victor
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
Thanks,
Victor
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.x ml")
Dim MyDocReceiver = XDocument.Load("C:\Receive r.xml")
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Do nor") 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("Do nor") Select el.Value).ToList
If Not CheckForItem.Contains("Me. TextBox1.T ext") Then
Dim xNew As XElement = New XElement("Donor", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestIDDonor(Larges tIDDonor.C ount - 1) + 1)))
MyDocDonor.Add(xNew)
End If
If Not CheckForItem.Contains("Me. TextBox2.T ext") Then
Dim xNew As XElement = New XElement("Receiver", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestIDReceiver(Lar gestIDRece iver.Count - 1) + 1)))
MyDocReceiver.Add(xNew)
End If
MyDocDonor.Save("C:\Donor. xml")
MyDocReceiver.Save("C:\Rec eiver.xml" )
End Sub
Thanks,
Victor
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.x
Dim MyDocReceiver = XDocument.Load("C:\Receive
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Do
LargestIDDonor.Sort()
Dim LargestIDReceiver = (From el In MyDocReceiver.Descendants(
LargestIDReceiver.Sort()
Dim CheckForItem = (From el In MyDocDonor.Descendants("Do
If Not CheckForItem.Contains("Me.
Dim xNew As XElement = New XElement("Donor", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestIDDonor(Larges
MyDocDonor.Add(xNew)
End If
If Not CheckForItem.Contains("Me.
Dim xNew As XElement = New XElement("Receiver", "Me.TextBox2.Text", New XAttribute("ID", CInt(LargestIDReceiver(Lar
MyDocReceiver.Add(xNew)
End If
MyDocDonor.Save("C:\Donor.
MyDocReceiver.Save("C:\Rec
End Sub
Thanks,
Victor
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_I D>
<Receiver>BEL</Receiver>
</Row>
<Row>
<Receiver_ID>2</Receiver_I D>
<Receiver>CAN</Receiver>
</Row>
<Row>
<Receiver_ID>3</Receiver_I D>
<Receiver>CZE</Receiver>
</Row>
<Row>
<Receiver_ID>4</Receiver_I D>
<Receiver>DNK</Receiver>
</Row>
<Row>
<Receiver_ID>5</Receiver_I D>
<Receiver>FRA</Receiver>
</Row>
<Row>
<Receiver_ID>6</Receiver_I D>
<Receiver>DEU</Receiver>
</Row>
<Row>
<Receiver_ID>7</Receiver_I D>
<Receiver>GRC</Receiver>
</Row>
<Row>
<Receiver_ID>8</Receiver_I D>
<Receiver>HUN</Receiver>
</Row>
<Row>
<Receiver_ID>9</Receiver_I D>
<Receiver>ITA</Receiver>
</Row>
</Root
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_I
<Receiver>BEL</Receiver>
</Row>
<Row>
<Receiver_ID>2</Receiver_I
<Receiver>CAN</Receiver>
</Row>
<Row>
<Receiver_ID>3</Receiver_I
<Receiver>CZE</Receiver>
</Row>
<Row>
<Receiver_ID>4</Receiver_I
<Receiver>DNK</Receiver>
</Row>
<Row>
<Receiver_ID>5</Receiver_I
<Receiver>FRA</Receiver>
</Row>
<Row>
<Receiver_ID>6</Receiver_I
<Receiver>DEU</Receiver>
</Row>
<Row>
<Receiver_ID>7</Receiver_I
<Receiver>GRC</Receiver>
</Row>
<Row>
<Receiver_ID>8</Receiver_I
<Receiver>HUN</Receiver>
</Row>
<Row>
<Receiver_ID>9</Receiver_I
<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
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
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("Do nor") Select el.@<ID>).ToList
Code:
Dim MyDocDonor = XDocument.Load("C:\Donor.x ml")
Dim MyDocReceiver = XDocument.Load("C:\Receive r.xml")
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Do nor") 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.x ml")
Dim MyDocReceiver = XDocument.Load("C:\Receive r.xml")
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Do nor") 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("Do nor") Select el.Value).ToList
If Not CheckForItem.Contains(Me.T extBox1.Te xt) Then
Dim xNew As XElement = New XElement("Row")
xNew.Add(New XElement("Donor_ID", CInt(LargestIDDonor(Larges tIDDonor.C ount - 1) + 1)))
xNew.Add(New XElement("Donor", Me.TextBox1.Text))
MyDocDonor.Add(xNew)
End If
Dim CheckForItem1 = (From el In MyDocDonor.Descendants("Re ceiver") Select el.Value).ToList
If Not CheckForItem1.Contains(Me. TextBox2.T ext) Then
Dim xNew As XElement = New XElement("Row")
xNew.Add(New XElement("Receiver_ID", CInt(LargestIDDonor(Larges tIDDonor.C ount - 1) + 1)))
xNew.Add(New XElement("Receiver", Me.TextBox2.Text))
MyDocReceiver.Add(xNew)
End If
MyDocDonor.Save("C:\Donor. xml")
MyDocReceiver.Save("C:\Rec eiver.xml" )
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("Do
Code:
Dim MyDocDonor = XDocument.Load("C:\Donor.x
Dim MyDocReceiver = XDocument.Load("C:\Receive
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Do
LargestIDDonor.Sort()
Dim LargestIDReceiver = (From el In MyDocReceiver.Descendants(
LargestIDReceiver.Sort()
Dim MyDocDonor = XDocument.Load("C:\Donor.x
Dim MyDocReceiver = XDocument.Load("C:\Receive
Dim LargestIDDonor = (From el In MyDocDonor.Descendants("Do
LargestIDDonor.Sort()
Dim LargestIDReceiver = (From el In MyDocReceiver.Descendants(
LargestIDReceiver.Sort()
Dim CheckForItem = (From el In MyDocDonor.Descendants("Do
If Not CheckForItem.Contains(Me.T
Dim xNew As XElement = New XElement("Row")
xNew.Add(New XElement("Donor_ID", CInt(LargestIDDonor(Larges
xNew.Add(New XElement("Donor", Me.TextBox1.Text))
MyDocDonor.Add(xNew)
End If
Dim CheckForItem1 = (From el In MyDocDonor.Descendants("Re
If Not CheckForItem1.Contains(Me.
Dim xNew As XElement = New XElement("Row")
xNew.Add(New XElement("Receiver_ID", CInt(LargestIDDonor(Larges
xNew.Add(New XElement("Receiver", Me.TextBox2.Text))
MyDocReceiver.Add(xNew)
End If
MyDocDonor.Save("C:\Donor.
MyDocReceiver.Save("C:\Rec
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
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!
ASKER
Hi,
Thank you for the code, I will try it and get back to you tomorrow.
Victor
Thank you for the code, I will try it and get back to you tomorrow.
Victor
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.x ml")
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.T extBox2.Te xt) Then
Dim xNew As XElement = New XElement("Donor")
xNew.Add(New XElement("ID", CInt(LargestID(LargestID.C ount - 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>
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.x
Dim LargestID = (From el In MyDoc.Descendants("Donor")
LargestID.Sort()
Dim CheckForItem = (From el In MyDoc.Descendants("Donor")
If Not CheckForItem.Contains(Me.T
Dim xNew As XElement = New XElement("Donor")
xNew.Add(New XElement("ID", CInt(LargestID(LargestID.C
xNew.Add(New XElement("Name", Me.TextBox2.Text.ToString.
MyDoc.Add(xNew)
MyDoc.Save("C:\Donor.xml")
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>
ASKER
Hi,
Can you please help me figure out what is wrong with the code in my previous post.
Thank You.
Victor
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")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It Worked!
Thank You!
Victor
Thank You!
Victor