views:

219

answers:

2

Hi,

I'm editing XML element with the following XML:

    <?xml version="1.0" encoding="utf-8"?>
<!--Test XML with LINQ to XML-->

<LabSerivceInfo>

  <LabService>
    <ServiceType>Copy</ServiceType>
    <Price>1</Price>
  </LabService>

  <LabService>
    <ServiceType>PrintBlackAndWhite</ServiceType>
    <Price>2</Price>
  </LabService>

</LabSerivceInfo>

Dim varServiceType = txtServiceType.Text.Trim

How to update the ServiceType and Price where ServiceType = varServiceType?

+1  A: 

Check these out:
http://msdn.microsoft.com/en-us/vbasic/bb688087.aspx
"LINQ to XML Samples" ~~ vb

http://msdn.microsoft.com/en-us/library/bb387091.aspx
"Samples (LINQ to XML)" ~~ c# and vb

http://msdn.microsoft.com/en-us/library/bb397965.aspx
"LINQ C# Samples"

more: via Google:

linq to xml samples
gerryLowry
A: 

You could use something like this:

Dim el = (From x In doc.XPathSelectElements("//*") _
          Where x.Value = varServiceType _
          Select x.Parent).FirstOrDefault()

The above code returns the <LabService> element.

Edited to add:

Hey, I can select the Price like this with condition

Dim query = (From s In xElement.Load(theXMLSource1).Descendants("LabService") _
            Where s.Element("ServiceType") = "Scan" _
            Select s.Element("Price").Value).FirstOrDefault() 

But, I can't figure it out how to update it yet. Can you share some code on this?

Using your sample:

Dim price = (From s In xElement.Load(theXMLSource1).Descendants("LabService") _
            Where s.Element("ServiceType") = "Scan" _
            Select s.Element("Price")).FirstOrDefault() 

price.Value += 1500
Paulo Santos
Hey, I can select the Price like this with condition Dim query = (From s In xElement.Load(theXMLSource1).Descendants("LabService") Where s.Element("ServiceType") = "Scan" Select s.Element("Price").Value).FirstOrDefaultBut, I can't figure it out how to update it yet. Can you share some code on this?
Narazana
Thank you for this. It really helps me a lot.
Narazana