Search code examples
xmlvb.netselectsinglenode

Null returned when selecting a node in XML document


New to XML. I have a 3rd party webservice that supplies an XML document that I have to update the element values and pass back. The core issue issue is I get an NullReferenceException error when calling the node.RemoveAll() method in the code below. I'm calling the RemoveAll() method because each element has the xsi:nil attribute when it is supplied to me, and if I don't remove it before updating the element value, the XML won't validate by the webservice.

The XML document supplied by the 3rd party webservice is as follows:

<?xml version="1.0" encoding="utf-16"?>
<TaskData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schema.sample.com/application/1/520800B">
  <Global>
    <RequestInfo xmlns="http://schema.sample.com/application/1/Types">
      <Requestor xsi:nil="true" />
      <Date_init xsi:nil="true" />
      <Shipto xsi:nil="true" />
      <Customer xsi:nil="true" />
      <Contact xsi:nil="true" />
      <Requestor_Email xsi:nil="true" />      
    </RequestInfo>    
   </Global>
  </TaskData>

Other solutions I've seen have used the XmlNamespaceManager, but I haven't been able to make it work. This xml document has a namespace specified for the TaskData element, and a different namespace for the RequestInfo element. I tried specifying the XmlNamespaceManager variable for each namespace, but got the same results....hovering over the nsmgr variable while in break mode reveals that the "children could not be evaluated" and that the DefaultNamespace property is an empty string.

Public Sub testxml()

    Dim doc As New XmlDocument
    doc.Load("c:\temp\sample.xml")

    Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
    nsmgr.AddNamespace("s", "http://schema.sample.com/application/1/520800B")

    Dim node As XmlNode = doc.SelectSingleNode("s:Requestor", nsmgr)
    node.RemoveAll()
    node.InnerText = "Your Name Goes Here"

    doc.Save("c:\temp\sample.xml")

End Sub

Solution

  • The problem is with this statement:

    doc.SelectSingleNode("s:Requestor", nsmgr)
    

    what you need to do is

    doc.SelectSingleNode("//s:Requestor",nsmgr)
    

    "s:Requestor" means give me the node underneath the current node name s:Requestor

    "//s:Requestor" means give me all nodes in the document named s:Requestor

    if you want to ignore the namespace you could do

    doc.SelectSingleNode("//*[local-name()='Requestor']")