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
320 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get row value in vb.net 4 17
XML Document XPath with Namespaces 3 26
.NET 2008 VB and C# 6 39
Calculate number of nights between two dates 5 53
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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