I have a script that creates an XML File if it already exits it will delete the old one and create a new one. Reason I do this is to create "Last Modity Date" to that day.
What im trying to do now seemed so simple but having issues to get it to work. Just a basic if statment I was thinking:
If the xmlFile.xml ( LastWriteTime = DateTime.Now ) Than
Just display a message in a label feild (Message.Text =" It did not Equal todays Date"
Else
Display Information from the XmlFile.xml
End If
Here is what I go so far:
Suggestion change made from matts post.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports System.IO
Imports System.Globalization
Partial Class busreport
Inherits System.Web.UI.Page
Public Sub LoadXMLFile()
Dim xmlPath As String = MapPath("BusReport.xml")
Dim xmlFile As FileInfo = New FileInfo(xmlPath)
Dim doc As New XmlDocument()
Dim FileInfo As New FileInfo(xmlPath)
Dim dtlastwrite As DateTime = FileInfo.LastWriteTime
Dim today As DateTime = DateTime.Now
If dtlastwrite.ToShortDateString() = today.ToShortDateString() Then
Message.Text ="Yes"
Else
Message.Text ="No"
End If
End Sub
End Class
I changed it up Im missing something but can not figure it out:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports System.IO
Imports System.Globalization
Partial Class busreport
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xmlDoc As New XmlDocument
xmlDoc.Load("BusReport.xml")
Dim xmlD As XmlNodeList = xmlDoc.GetElementsByTagName("Date")
Dim xmlds = xmlD.ToString()
Dim d = DateTime.Now.ToShortDateString()
If xmlds = d Then
Message.Text = "Yes"
Message2.Text = d
Message3.Text = xmlds
Else
Message.Text = "No"
Message2.Text = d
Message3.Text = xmlds
End If
End Sub
End Class
No
11/10/2010
System.Xml.XmlElementList
I want the System.Xml.XmlElementListto egual the XML date Value.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports System.IO
Imports System.Globalization
Partial Class busreport
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xmlDoc As New XmlDocument
Dim reader As New XmlTextReader("BusReport.xml")
Dim xmlD = xmlDoc.GetElementsByTagName("Date")
Dim xmlds = xmlD.ToString()
Dim d = DateTime.Now.ToShortDateString()
If xmlds = d Then
Message.Text = "Yes"
Message2.Text = d
Message3.Text = xmlds
Else
Message.Text = "No"
Message2.Text = d
Message3.Text = xmlds
End If
End Sub
End Class
I see what you are asking. On line 18, the GetElementsByTagName is returning all of the nodes in the XML document, so you need to either loop through them or specify that you just want the first node. Try this on line 19...
vipernet
Member
50 Points
29 Posts
Check FileInfo.LasteWriteTime = Date.Now
Nov 09, 2010 06:24 PM|LINK
Good day all,
I have a script that creates an XML File if it already exits it will delete the old one and create a new one. Reason I do this is to create "Last Modity Date" to that day.
What im trying to do now seemed so simple but having issues to get it to work. Just a basic if statment I was thinking:
If the xmlFile.xml ( LastWriteTime = DateTime.Now ) Than
Just display a message in a label feild (Message.Text =" It did not Equal todays Date"
Else
Display Information from the XmlFile.xml
End If
Here is what I go so far:
Suggestion change made from matts post.
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Xml Imports System.IO Imports System.Globalization Partial Class busreport Inherits System.Web.UI.Page Public Sub LoadXMLFile() Dim xmlPath As String = MapPath("BusReport.xml") Dim xmlFile As FileInfo = New FileInfo(xmlPath) Dim doc As New XmlDocument() Dim FileInfo As New FileInfo(xmlPath) Dim dtlastwrite As DateTime = FileInfo.LastWriteTime Dim today As DateTime = DateTime.Now If dtlastwrite.ToShortDateString() = today.ToShortDateString() Then Message.Text ="Yes" Else Message.Text ="No" End If End Sub End ClassAZMatt
Star
10648 Points
1896 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 09, 2010 08:44 PM|LINK
On lines 22 and 23, you can avoid all the CultureInfo by using dtlastwrite.ToShortDateString() = today.ToShortDateString()
Matt
vipernet
Member
50 Points
29 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 10, 2010 01:44 AM|LINK
Cool, Thanks Matt. I will try that out
Any idea why its not showing an output?
AZMatt
Star
10648 Points
1896 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 10, 2010 03:23 PM|LINK
Not sure...try running in Debug mode with a breakpoint on the If statement and see if it ever makes it to the breakpoint.
Matt
vipernet
Member
50 Points
29 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 10, 2010 05:31 PM|LINK
I changed it up Im missing something but can not figure it out:
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Xml Imports System.IO Imports System.Globalization Partial Class busreport Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim xmlDoc As New XmlDocument xmlDoc.Load("BusReport.xml") Dim xmlD As XmlNodeList = xmlDoc.GetElementsByTagName("Date") Dim xmlds = xmlD.ToString() Dim d = DateTime.Now.ToShortDateString() If xmlds = d Then Message.Text = "Yes" Message2.Text = d Message3.Text = xmlds Else Message.Text = "No" Message2.Text = d Message3.Text = xmlds End If End Sub End Classvipernet
Member
50 Points
29 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 10, 2010 05:41 PM|LINK
the output =:
No
11/10/2010
System.Xml.XmlElementList
I want the System.Xml.XmlElementListto egual the XML date Value.
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Xml Imports System.IO Imports System.Globalization Partial Class busreport Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim xmlDoc As New XmlDocument Dim reader As New XmlTextReader("BusReport.xml") Dim xmlD = xmlDoc.GetElementsByTagName("Date") Dim xmlds = xmlD.ToString() Dim d = DateTime.Now.ToShortDateString() If xmlds = d Then Message.Text = "Yes" Message2.Text = d Message3.Text = xmlds Else Message.Text = "No" Message2.Text = d Message3.Text = xmlds End If End Sub End ClassAZMatt
Star
10648 Points
1896 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 10, 2010 09:00 PM|LINK
I see what you are asking. On line 18, the GetElementsByTagName is returning all of the nodes in the XML document, so you need to either loop through them or specify that you just want the first node. Try this on line 19...
Dim xmlds = xmlD(0).InnerXml
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.getelementsbytagname.aspx
Matt
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 12, 2010 01:14 AM|LINK
Yes, GetElementsByTagName attention its name in a form of plural! (Elements), this means it will return a collection of
data.
And now you can try this:
Dim xmlds = xmlD(0-based index).InnerText //To get a time from a specific tag
And use CType(xmlds,data) to make a conversion.
vipernet
Member
50 Points
29 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 15, 2010 02:37 PM|LINK
Error : System.NullReferenceException: Object reference not set to an instance of an object.
AZMatt
Star
10648 Points
1896 Posts
Re: Check FileInfo.LasteWriteTime = Date.Now
Nov 15, 2010 03:20 PM|LINK
Could you post the XML that you are trying to read?
Matt