• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

Update simple XML using LINQ

xml snipet
<AddBook>
  <Contact Fname="John" Lname="Doe">
    <Address>1234 Some Street</Address>
    <City>Milan</City>
    <State>TN</State>
    <HomePhone>731-123-4567</HomePhone>
    <CellPhone>731-321-7654</CellPhone>
    <Notes>No one really knows who he is!</Notes>
  </Contact>
  <Contact Fname="Jane" Lname="Smith">
    <Address>4321 Another Street</Address>
    <City>Milan</City>
    <State>TN</State>
    <HomePhone>731-555-4567</HomePhone>
    <CellPhone>731-555-9786</CellPhone>
    <Notes>No one really knows who She is!</Notes>
  </Contact>
</AddBook>

I can add a new contact but how do I update and existing one?
0
vbMarkO
Asked:
vbMarkO
  • 5
3 Solutions
 
vbMarkOAuthor Commented:
My Apologies I forgot I should be clear that I use vb.net express 2010 I do not know c#

Not sure how to convert these ... but I am trying ...
0
 
sachinpatil10dCommented:
Check this site for code conversion

http://converter.telerik.com/
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
vbMarkOAuthor Commented:
checking these out to see hwo to make them work ... in the mean time I took a stab at this .. perhaps you might tell if I am on right track


Dim doc = XDocument.Load(myPath)

        For Each mNod In doc.<AddBook>.<Contact>
            If mNod.<Contact>.@Fname = txtfName.Text And mNod.<Contact>.@Lname = txtLname.Text Then
                mNod.Attribute("Fname").Value = txtfName.Text
                mNod.Attribute("Lname").Value = txtLname.Text

            End If
        Next




        doc.Save(myPath)
        lstMem.Items.Clear()
        Call Refresh()

Open in new window

0
 
vbMarkOAuthor Commented:
Here is how I add new contact using linq ... I knowI should be able to update it doing something similar

Dim doc = XDocument.Load(myPath)

        Dim xEl As XElement = doc.Root
        Dim xCon As New XElement("Contact", New XAttribute("Fname", txtfName.Text), New XAttribute("Lname", txtLname.Text), _
                                 New XElement("Address", txtAdd.Text), _
                                 New XElement("City", txtCity.Text), _
                                 New XElement("State", txtState.Text), _
                                 New XElement("HomePhone", txtHomeNum.Text), _
                                 New XElement("CellPhone", txtCellNum.Text), _
                                 New XElement("Notes", rtb1.Text))

        xEl.Add(xCon)
        doc.Save(myPath)

Open in new window

0
 
vbMarkOAuthor Commented:
I found what I was doing wrong and what was throwing me ....
This Code works ... I am happy with it but I will award points to anyone Using LINQ can show me another way ....

My problem was fixed when I used the seleteditem of the listbox which was first and last name DUH!!
  I was trying to use txtFname.text to both change or update the xml file and alos search ... So it wasnt finding anything to update ... Man dont know what I was thinking.
Here is my code ... I would be very interested still in LINQ if it can be done better or more stream lined I love to learn ....
Dim doc = XDocument.Load(myPath)
        Dim names() As String = Split(lstMem.SelectedItem, " ")
        Dim fName As String = names(0)
        Dim Lname As String = names(1)
        Dim myCon = From xe In doc...<Contact> _
                    Where xe.@Fname = fName _
                    Where xe.@Lname = Lname _
                    Select xe

        For Each itm In myCon
            itm.Attribute("Fname").Value = txtfName.Text
            itm.Attribute("Lname").Value = txtLname.Text
            itm.<Address>.Value = txtAdd.Text
            itm.<City>.Value = txtCity.Text
            itm.<State>.Value = txtState.Text
            itm.<HomePhone>.Value = txtHomeNum.Text
            itm.<CellPhone>.Value = txtCellNum.Text
            itm.<Notes>.Value = rtb1.Text

        Next

        doc.Save(myPath)
        lstMem.Items.Clear()
        Call Refresh()

Open in new window

0
 
vbMarkOAuthor Commented:
Dont know c# ... the converter helped and it led to me finding what would work
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.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now