?
Solved

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

Posted on 2011-09-07
13
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 
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 2000 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
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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