I have been working for hours trying to get an XPath query to work.
At first, I thought it was the namespaces causing me issues, but I
added an xmlNameSpacesManager and still cannot get my code to work. I
keep getting a NullReferenceException. Any help would be appreciated
as this has been really frustrating. -Thanks
The following is my VB.Net code:
Dim xmlDoc As New System.Xml.XmlDocument
Dim xmlNode As System.Xml.XmlNode
Dim xmlNSM As System.Xml.XmlNamespaceManager
Dim FromID As String
'load the xml document
xmlDoc.Load("C:\my.xml")
'establish the namespace manager
xmlNSM = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)
For Each attr As System.Xml.XmlAttribute In
xmlDoc.SelectSingleNode("/*").Attributes
If attr.Prefix = "xmlns" Then
xmlNSM.AddNamespace(attr.LocalName, attr.Value)
End If
Next attr
'set default namespace
xmlNSM.AddNamespace("", "http://www.icsm.com/icsmxml")
xmlNSM.PushScope()
'move to the root node
xmlNode = xmlDoc.DocumentElement
'find the FROM id
Try
FromID = xmlNode.SelectSingleNode("./Header/MsgDelivery/From/
Credential/Identity", xmlNSM).InnerText.Trim.ToUpper
Catch ex As Exception
Throw ex
End Try
The xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ICSMXML xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://
www.icsm.com/icsmxml">
<Header>
<MsgDelivery>
<To>
<Credential>
<Domain>ToDomain</Domain>
<Identity>ToIdentity</Identity>
</Credential>
</To>
<From>
<Credential>
<Domain>FromDomain</Domain>
<Identity>FromIdentity</Identity>
</Credential>
</From>
<Sender>
<Credential>
<Domain>SenderDomain</Domain>
<Identity>SenderIdentity</Identity>
</Credential>
</Sender>
</MsgDelivery>
<MsgHeader>
<MessageId>test</MessageId>
<Timestamp>00:00:00</Timestamp>
<ResponseTo></ResponseTo>
<DocumentId>DocumentID</DocumentId>
</MsgHeader>
<ConversationState>
<AgreementId>Agreement</AgreementId>
<ConversationId>Conversation</ConversationId>
</ConversationState>
</Header>
<Request>
<Message>This is my Message</Message>
</Request>
</ICSMXML>
|