?
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
Medium Priority
?
332 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
[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
  • 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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