Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on 

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
Visual Basic.NETXMLASP.NET

Avatar of undefined
Last Comment
Victor Charles
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Hello,

This question is marked Neglected. Does that mean it was asked imprioperly?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

You would need to change the InnerText for an XmlNode, and then save the XmlDocument.
Avatar of Victor  Charles

ASKER

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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.).
Avatar of Victor  Charles

ASKER

Can you please send me an example, Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Victor  Charles

ASKER

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
Avatar of Victor  Charles

ASKER

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
Avatar of Victor  Charles

ASKER

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
Avatar of Victor  Charles

ASKER

Thanks You!
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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.
Avatar of Victor  Charles

ASKER

Thank you.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo