Welcome to Vishwa's blog - Technology, Spirituality and More...

Parsing XML with multiple namespaces

You may come across a situation when you will have to read a XML document containing name spaces and assign values to it. Here is sample which includes multiple namespaces with SOAP envelope xml document.


<s:Envelope xmlns:s="">
    <GetRecord xmlns="">
      <Request xmlns:a="" xmlns:i="">


Function which reads and assign values 

Private Function ReadAndAssignRequestXML(ByVal recordID As String) As String
Dim xmlDoc As New XmlDocument
Dim InputSOAPXML As String = String.Empty
Dim xmlFileName As String = "C:\Test.xml"

Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("a", "")
nsmgr.AddNamespace("s", "")
nsmgr.AddNamespace("l", "")

xmlDoc.SelectSingleNode("/s:Envelope/s:Body/l:GetRecord/l:Request/l:LoginData/l:UserID", nsmgr).InnerText = "myUserID"
xmlDoc.SelectSingleNode("/s:Envelope/s:Body/l:GetRecord/l:Request/l:LoginData/l:Password", nsmgr).InnerText = "PASS"
xmlDoc.SelectSingleNode("/s:Envelope/s:Body/l:GetRecord/l:Request/a:RecordID", nsmgr).InnerText = recordID

InputSOAPXML = xmlDoc.OuterXml

Catch ex As Exception
InputSOAPXML = ex.Message
End Try

Return InputSOAPXML
End Function