By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

Check if an element in a XmlDocument exists

P: n/a
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
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 = ""
End If
Return xmldoc
End Function
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a

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")

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! -
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.