Solved

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

Posted on 2011-09-07
8
309 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:vcharles
  • 4
  • 3
8 Comments
 

Author Comment

by:vcharles
ID: 36501790
Hello,

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

Thanks,

Victor
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36504117
You need to create a new XElement, and append it to a parent element, and save.
0
 

Author Comment

by:vcharles
ID: 36505901
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
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 36506759
I am not sure that I understand what you are asking for...
0
 

Author Comment

by:vcharles
ID: 36506934
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36507231
It is confusing to use XmlDocument and LINQ-to-XML (XDocument/XElement).  I would stick with LINQ-to-XML, as it has a lot more power than the XmlDocument.
0
 

Author Closing Comment

by:vcharles
ID: 36581862
Thank You!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

16 Experts available now in Live!

Get 1:1 Help Now