• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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

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
vcharles
Asked:
vcharles
  • 8
  • 4
1 Solution
 
vcharlesAuthor Commented:
Hello,

This question is marked Neglected. Does that mean it was asked imprioperly?
0
 
Bob LearnedCommented:
You would need to change the InnerText for an XmlNode, and then save the XmlDocument.
0
 
vcharlesAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Bob LearnedCommented:
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
 
vcharlesAuthor Commented:
Can you please send me an example, Thanks.
0
 
Bob LearnedCommented:
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
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
Thanks You!
0
 
Bob LearnedCommented:
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
 
vcharlesAuthor Commented:
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.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now