Check if an element in a XmlDocument exists

G

Guest

I have an XML file that has a date element. I want to change this date to
unix notation. This works, but if the XML does not have the element I cannot
assign the DateField the value. How can I check first if the node exists
before I try to replace it?


Public Function ConvertDates(ByVal xmldoc As XmlDocument) As XmlDocument
Dim nodelist As XmlNodeList
Dim node As XmlNode
Dim DateField As String

nodelist = xmldoc.SelectNodes("/REQUEST/IMPORT/DOCUMENT/FIELD")
For Each node In nodelist
If node.Attributes.GetNamedItem("CODE").Value = "BASE64" Then
Try
DateField = node.Item("DATETIME").InnerText()
Exit Function
Catch ex As Exception
End Try
Dim newElem As XmlNode
newElem = xmldoc.CreateNode(XmlNodeType.Element, "DATIME", "")
newElem.InnerText = xmlsvr.toUnixDateTime(DateField)
node.Item("DATETIME").InnerText = ""
node.Item("DATETIME").AppendChild(newElem)
End If
Next
Return xmldoc
End Function
 
P

Peter Huang [MSFT]

Hi

I think you may try to first get the Node of DATETIME, it that is null,
then it did not exist.
XmlNode nd = node.Item("DATETIME")
if nd = Nothing then
msgbox("DATETIME did not exist")
endif

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top