开发者

Update XML element with LINQ to XML

开发者 https://www.devze.com 2022-12-22 20:03 出处:网络
I\'开发者_如何学运维m editing XML element with the following XML: <?xml version=\"1.0\" encoding=\"utf-8\"?>

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?


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号