Solved

Help with amending values of Receiver.xml and Donor.xml files from two combo boxes

Posted on 2011-09-07
13
312 Views
Last Modified: 2012-05-12
Hello,

I am loading data from Receiver.xml and Donor.xml files to two combo boxes, when I make a change to the “Name” element in Receiver and Donor from the combox boxes, how do I save the modified “Name” element. Below is the code I am using to load the combo boxes, the DropDownStyle property was set to Simple to make it look like a Textbox.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DonorStream.Load(Application.StartupPath & "/donor.xml") 'My.Resources.Donor)
        RecStream.Load(Application.StartupPath & "/receiver.xml") 'My.Resources.Receiver)
        LinkStream.Load(Application.StartupPath & "/link.xml") 'My.Resources.Link)
        ReadLink()
End Sub

Public Sub ReadLink()
        FillCombos()
        btnNext.PerformClick()
    End Sub

    Public Sub FillCombos()
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

        Dim donorlst As New DataTable()
        donorlst.Columns.Add("ID")
        donorlst.Columns.Add("Name")
        Dim reclst As New DataTable
        reclst.Columns.Add("ID")
        reclst.Columns.Add("Name")

        For Each node As XmlNode In RecName
            reclst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
        Next

        For Each node As XmlNode In DonorName
            donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
        Next

        CmbDonor.DataSource = donorlst
        CmbDonor.DisplayMember = "Name"
        CmbDonor.ValueMember = "ID"

        CmbRec.DataSource = reclst
        CmbRec.DisplayMember = "Name"
        CmbRec.ValueMember = "ID"

Open in new window


By the way, I'm curently 6 hous ahead of US EST, my reply might be during odd hours, These questions may seem similar in the future because I will also need to develop web version with the same features using ASP.NET.

Thanks,
Victor
0
Comment
Question by:vcharles
  • 8
  • 4
13 Comments
 

Author Comment

by:vcharles
ID: 36501777
Hello,

This question is marked Neglected. Does that mean it was asked imprioperly?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36503978
You would need to change the InnerText for an XmlNode, and then save the XmlDocument.
0
 

Author Comment

by:vcharles
ID: 36505949
Hi,

I'm not sure I understand, do you mean change Add to Save?

 For Each node As XmlNode In DonorName
            donorlst.Rows.Save(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
        Next

Unfortunately I have to wait till tomorrow to try the code in the office tomorrow.

Thanks,

Victor
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36506581
No, I mean that you would need to have a reference to the corresponding XmlNode, so that you could change the InnerText property, and then write the XML document back to where you got the contents from (like a file, or a database, etc.).
0
 

Author Comment

by:vcharles
ID: 36506952
Can you please send me an example, Thanks.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36507228
Hopefully this can provide you enough information to get started:

Update nodes and attributes in a Xml document
http://techiecocktail.blogspot.com/2009/04/update-nodes-and-attributes-in-xml.html
Private Shared Sub Main(args As String())
	Dim fileName As String = "d:\books.xml"

	'read the Xml file
	Dim _xmlTextReader As New XmlTextReader(fileName)

	'load the Xml file into the XmlDocument object
	Dim _xmlDocument As New XmlDocument()
	_xmlDocument.Load(_xmlTextReader)

	'Note: Close the reader object to release the xml file. Else while saving you will get an error that it is 
	'being used by another process.
	_xmlTextReader.Close()

	'update the element (price of the book - asp.net)
	updateElement(fileName, _xmlDocument)
End Sub

Private Shared Sub updateElement(fileName As String, xmlDoc As XmlDocument)
	'get the root element of the document
	Dim rootElement As XmlElement = xmlDoc.DocumentElement

	'create a pointer/reference to the price node for the book named 'asp.net'
	Dim priceNode As XmlNode = rootElement.SelectSingleNode("/books/book[@name='asp.net']/price")
	priceNode.InnerText = "35"

	'save the Xml
	xmlDoc.Save(fileName)
End Sub

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vcharles
ID: 36510290
Hi,

I modified the code in Part B and everyting is working OK. However when I tried the modify the code for Part A below: I receive the following error: Type 'XDocument' is not defined
On line: Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
How do I fix this error?

Code Part A:
Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
         an = MsgBox("Are you sure you want to Add this record?", MsgBoxStyle.YesNo, "Warning before Attempting to Save Data")
        If an = MsgBoxResult.Yes Then
            Dim xml As String = LinkStream.InnerXml
            Dim newline As String = Environment.NewLine
            Dim replacement As String = "<Link><LinkID>" & (LinkID.Last() + 1) & "</LinkID><ReceiverID>" & CmbRec.SelectedValue & "</ReceiverID><DonorID>" & CmbDonor.SelectedValue & "</DonorID></Link>"
            LinkStream.InnerXml = LinkStream.InnerXml.Replace("</Root>", "") & replacement & "</Root>"
            LinkStream.Save(Application.StartupPath & "/Link.xml")
            Dim cnt As Integer = LinkStream.SelectNodes("Root/Link").Count
            txtTotalRecords.Text = cnt
            btnNext.PerformClick()
            MsgBox("Data was Saved")
        End If

Below is theh working code for Part B:

 'Donor
        Dim MyDocDonor As XDocument = XDocument.Load(Application.StartupPath & "/Donor.xml")
        Dim LargestDonorID = (From el In MyDocDonor.Descendants("Donor") Select CInt(el.<DonorID>.Value)).ToList
        LargestDonorID.Sort()
        Dim CheckForItem = (From el In MyDocDonor.Descendants("Donor").Elements("Name") Select el.Value).ToList
        If Not CheckForItem.Contains(Me.TextBox1.Text.ToString) 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(Application.StartupPath & "/Donor.xml")
            End If

        'Receiver()
        Dim MyDocReceiver As XDocument = XDocument.Load(Application.StartupPath & "/Receiver.xml")
        Dim LargestReceiverID = (From el In MyDocReceiver.Descendants("Receiver") Select CInt(el.<ReceiverID>.Value)).ToList
        LargestReceiverID.Sort()
        Dim CheckForItem1 = (From el In MyDocReceiver.Descendants("Receiver").Elements("Name") Select el.Value).ToList
        If Not CheckForItem1.Contains(Me.Textbox2.Text.ToString) Then
            Dim xNew1 As XElement = New XElement("Receiver")
            xNew1.Add(New XElement("ReceiverID", (LargestReceiverID.Last() + 1)))
            xNew1.Add(New XElement("Name", Me.Textbox2.Text.ToString))
            MyDocReceiver.Root.Add(xNew1)
            MyDocReceiver.Save(Application.StartupPath & "/Receiver.xml")
            End If

        'Link
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort()
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))
        xNewLink.Add(New XElement("ReceiverID", (LargestReceiverID.Last() + 1)))
        xNewLink.Add(New XElement("DonorID", (LargestDonorID.Last() + 1)))
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 36510368
Hi,

I'm using Xdocument in another project with the same declarations but I'm not getting the error "'XDocument' is not defined"  message that I'm receiving in this project. I can't tell why that is the case, both projects are are also in VS2010.
Do yoiu have any ideas why the error is occuring in this particular project?

Victor
0
 

Author Comment

by:vcharles
ID: 36511176
Hello,

I transfered the code to the project where Xdocument did not trigger an error and now both parts of my questions are resolved. Thanks Yoiu Very much.

Victor
0
 

Author Closing Comment

by:vcharles
ID: 36511200
Thanks You!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36512919
BTW, you need to add a reference to System.Linq.Xml.dll in order to see the XDocument class.  That is not added by default.
0
 

Author Comment

by:vcharles
ID: 36512968
Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now