Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
338 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

597 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