Update XML element with LINQ to XML in VB.NET

Posted by Bayonian on Stack Overflow See other posts from Stack Overflow or by Bayonian
Published on 2010-03-15T20:04:50Z Indexed on 2010/03/15 20:09 UTC
Read the original article Hit count: 1012

Filed under:
|
|

Hi,

I'm trying to update an element in the XML document below:

Here's the code:

Dim xmldoc As XDocument = XDocument.Load(theXMLSource1)
        Dim ql As XElement = (From ls In xmldoc.Elements("LabService") _
                Where CType(ls.Element("ServiceType"), String).Equals("Scan") _
                Select ls.Element("Price")).FirstOrDefault


        ql.SetValue("23")
        xmldoc.Save(theXMLSource1)

Here's the XML file:

<?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>

But, I got this error message:

Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Error line:ql.SetValue("23")

Can you show me what the problem is? Thank you.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about linq-to-xml