1

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?

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.