Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

1. How to add LinkID element to Link.xml while adding new data in textbox and selecting data in a combo box?.

Hello,

I have two similar questions related to my subject for adding data to two textboxes and to two combo boxes.
First , I am using the code in Part A to add data selected from two combox  boxes to a link.xml file. I’ve now added a linkID element to the link.xml file. How do I modify the code in Part A to also add a LinkID element and increment its value based on the last LinkID’s value?
Second, I am using the code in Part B to save data entered in two textboxes, I’ve also added a LinkID element to the link.xml file. How do I modify the code in Part B to also add a LinkID element and increment its value based on the last LinkID’s value?

My actual project contains more than two textbox/combobox controls, I’m only using two as an example assuming if it works for two controls it will also work for additional controls.

Code Part A:
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><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
        If an = MsgBoxResult.No Then
            MsgBox("Record was not Added")
        End If 

Open in new window


Link.xml:
<?xml version="1.0" standalone="yes"?>
<Root>
  <Link>
  <LinkID>0</LinkID>    
<ReceiverID>2</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
  <Link>
  <LinkID>1</LinkID>    
    <ReceiverID>1</ReceiverID>
    <DonorID>2</DonorID>
  </Link>
  <Link>
   <LinkID>2</LinkID>    

    <ReceiverID>3</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
  <Link>
    <LinkID>3</LinkID>    
    <ReceiverID>2</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
</Root>

Open in new window


Code Part B:
Dim MyDoc As XDocument = XDocument.Load(Application.StartupPath & "/Donor.xml")
        Dim LargestID = (From el In MyDoc.Descendants("Donor") Select CInt(el.<DonorID>.Value)).ToList
        LargestID.Sort()
        Dim CheckForItem = (From el In MyDoc.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", (LargestID.Last() + 1)))
            xNew.Add(New XElement("Name", Me.TextBox1.Text.ToString))
            MyDoc.Root.Add(xNew)
        End If
        MyDoc.Save(Application.StartupPath & "/donor.xml")
        'Receiver()
        Dim MyDoc1 As XDocument = XDocument.Load(Application.StartupPath & "/Receiver.xml")
        Dim LargestID1 = (From el In MyDoc1.Descendants("Receiver") Select CInt(el.<ReceiverID>.Value)).ToList
        LargestID1.Sort()
        Dim CheckForItem1 = (From el In MyDoc1.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", (LargestID1.Last() + 1)))
            xNew1.Add(New XElement("Name", Me.Textbox2.Text.ToString))
            MyDoc1.Root.Add(xNew1)
        End If
        MyDoc1.Save(Application.StartupPath & "/Receiver.xml")

        'Update Link.xml
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("ReceiverID", (LargestID.Last() + 1)))
        xNewLink.Add(New XElement("DonorID", (LargestID1.Last() + 1)))
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

Open in new window


Thanks,

Victor
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Hello,

This question is marked neglected, does it mean I need to rephrase the question.

Thanks,

Victor
Avatar of Bob Learned
You need to create a new XElement, and append it to a parent element, and save.
Hi,

The code in Part A is not using XElement, it's using:

 Dim replacement As String = "<Link><ReceiverID>" & CmbRec.SelectedValue & "</ReceiverID><DonorID>" & CmbDonor.SelectedValue & "</DonorID></Link>"
            LinkStream.InnerXml = LinkStream.InnerXml.Replace("</Root>", "") & replacement & "</Root>"
           
Can we include the values for LinkID by modifying the the code above?

But I think this is what I need to do in code for Part B:
:
Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml") *****New

 Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<LinkID>.Value)).ToList
        LinkID.Sort() *****New

'Update Link.xml
        Dim MyDocLink As XDocument = XDocument.Load(Application.StartupPath & "/Link.xml")
        Dim xNewLink As XElement = New XElement("Link")
        xNewLink.Add(New XElement("LinkID", (LinkID.Last() + 1)))   ********New
        xNewLink.Add(New XElement("ReceiverID", (LargestID.Last() + 1)))
        xNewLink.Add(New XElement("DonorID", (LargestID1.Last() + 1)))
        MyDocLink.Root.Add(xNewLink)
        MyDocLink.Save(Application.StartupPath & "/Link.xml")

Is that the right approach for part B?.

Thanks,

Victor
I am not sure that I understand what you are asking for...
Hi
I will try the code i was asking about
for part b and get back to you tomorrow. You mentioned adding a new element  in the code for part  A but the code does not have any xelement.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You!