VB.NET add an element to the XML document with LINQ to XML

Posted by Bayonian on Stack Overflow See other posts from Stack Overflow or by Bayonian
Published on 2010-03-15T13:31:03Z Indexed on 2010/03/17 3:51 UTC
Read the original article Hit count: 482

Filed under:
|

Hi,

I'm adding an element to existing XML doc with the following code:

        Dim theXMLSource As String = Server.MapPath("~/Demo/") & "LabDemo.xml"
    Dim nodeElement As XElement

    Dim attrAndValue As XElement = _
        <LabService>
            <ServiceType>
                <%= txtServiceType.Text.Trim %>
            </ServiceType>
            <Level>
                <%= txtLevel.Text.Trim %>
            </Level>
        </LabService>

    nodeElement.Add(New XElement(attrAndValue))
    nodeElement.Save(theXMLSource)

It makes error like this:

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

Object reference not set to an instance of an object.

Error line: nodeElement.Add(New XElement(attrAndValue))

I debugged it but I couldn't get the error yet. Can you show what the problem is? Thank you

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about linq-to-xml