VB.NET add an element to the XML document with LINQ to XML
- by Bayonian
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