Victor Charles
asked on
Help with using Gridview to save data in XML file
Hi,
I have many questions on how to solve an issue, below are my following questions:
After I loading the grid with my Donorxml file.
1. How do I only add data in the grid if it doesn't already exist in Donor.xml? (If it does use the existing data to avoiding adding duplicate data)
2. If the data i enter in the grid is new, how do I save it in Donor.xml with a new ID?
3. When I select multiple rows in the grid and press Save, how do i save the IDs of the rows selected to my link.xml file
For example when I enter:
BEL
CAN
USA
In Grid1 ( three rows of data)
I need to save them in Donor.xml while I create new IDs as shown below:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Donor>
<DonorID>1</DonorID>
<Name>BEL</Name>
</Donor>
<Donor>
<DonorID>2</DonorID>
<Name>CAN</Name>
</Donor>
<Donor>
<DonorID>3</DonorID>
<Name>USA</Name>
</Donor>
</Root>
And at the same time save the newly created IDs to one record in my Link.xmk filke
<Link>
<LinkID>1</LinkID>
<DonorID>1,2,3</DonorID>
</Link>
How do I modify the code to use a grid for multiple entries and saving their IDs to link.xml?
Code: (This code works for entering a single country in a textbox and saving it to Donor.xml and it's ID to Link.xml, as mentioned, now I need to be able to save multiple IDs in Link.xml after selecting multiple rows in a Grid)
'Donor
Dim MyDocDonor As XDocument = XDocument.Load(Application .StartupPa th & "/Donor.xml")
Dim LargestDonorID = (From el In MyDocDonor.Descendants("Do nor") Select CInt(el.<DonorID>.Value)). ToList
LargestDonorID.Sort()
Dim CheckForItem = (From el In MyDocDonor.Descendants("Do nor").Elem ents("Name ") Select el.Value).ToList
If Not CheckForItem.Contains(Me.T extBox1.Te xt.ToStrin g) Then
Dim xNew As XElement = New XElement("Donor")
xNew.Add(New XElement("DonorID", (LargestDonorID.Last() + 1)))
xNew.Add(New XElement("Name", Me.TextBox1.Text.ToString) )
MyDocDonor.Root.Add(xNew)
MyDocDonor.Save(Applicatio n.StartupP ath & "/Donor.xml")
End If
'Link
Dim MyDocLink As XDocument = XDocument.Load(Application .StartupPa th & "/Link.xml")
Dim LinkID = (From el In MyDocLink.Descendants("Lin k") Select CInt(el.<LinkID>.Value)).T oList
LinkID.Sort()
Dim xNewLink As XElement = New XElement("Link")
xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
xNewLink.Add(New XElement("DonorID", (LargestDonorID.Last() + 1)))
MyDocLink.Root.Add(xNewLin k)
MyDocLink.Save(Application .StartupPa th & "/Link.xml")
Thanks,
Victor
I have many questions on how to solve an issue, below are my following questions:
After I loading the grid with my Donorxml file.
1. How do I only add data in the grid if it doesn't already exist in Donor.xml? (If it does use the existing data to avoiding adding duplicate data)
2. If the data i enter in the grid is new, how do I save it in Donor.xml with a new ID?
3. When I select multiple rows in the grid and press Save, how do i save the IDs of the rows selected to my link.xml file
For example when I enter:
BEL
CAN
USA
In Grid1 ( three rows of data)
I need to save them in Donor.xml while I create new IDs as shown below:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
<Donor>
<DonorID>1</DonorID>
<Name>BEL</Name>
</Donor>
<Donor>
<DonorID>2</DonorID>
<Name>CAN</Name>
</Donor>
<Donor>
<DonorID>3</DonorID>
<Name>USA</Name>
</Donor>
</Root>
And at the same time save the newly created IDs to one record in my Link.xmk filke
<Link>
<LinkID>1</LinkID>
<DonorID>1,2,3</DonorID>
</Link>
How do I modify the code to use a grid for multiple entries and saving their IDs to link.xml?
Code: (This code works for entering a single country in a textbox and saving it to Donor.xml and it's ID to Link.xml, as mentioned, now I need to be able to save multiple IDs in Link.xml after selecting multiple rows in a Grid)
'Donor
Dim MyDocDonor As XDocument = XDocument.Load(Application
Dim LargestDonorID = (From el In MyDocDonor.Descendants("Do
LargestDonorID.Sort()
Dim CheckForItem = (From el In MyDocDonor.Descendants("Do
If Not CheckForItem.Contains(Me.T
Dim xNew As XElement = New XElement("Donor")
xNew.Add(New XElement("DonorID", (LargestDonorID.Last() + 1)))
xNew.Add(New XElement("Name", Me.TextBox1.Text.ToString)
MyDocDonor.Root.Add(xNew)
MyDocDonor.Save(Applicatio
End If
'Link
Dim MyDocLink As XDocument = XDocument.Load(Application
Dim LinkID = (From el In MyDocLink.Descendants("Lin
LinkID.Sort()
Dim xNewLink As XElement = New XElement("Link")
xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
xNewLink.Add(New XElement("DonorID", (LargestDonorID.Last() + 1)))
MyDocLink.Root.Add(xNewLin
MyDocLink.Save(Application
Thanks,
Victor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You!
ASKER
Thank you for the links but they did not address my issue with xml and vb.bet, can you please help me with the code I posted in muy initial post, to help prevent entering duplicate data in a GriView using XML as a datasource?
Thanks,
Victor