Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET DateTime to XML DateTime

Status
Not open for further replies.

chadau

Programmer
Nov 14, 2002
155
US
I would like to fill a dateTime attribute in XML with a datetime value in VB.NET. Is there any easy method of conversion?

XML File

<xs:attribute name="time" type="xs:dateTime"/>
 
If you're using a XmlTextWriter:
Code:
_createdOnUTC = DateTime.UtcNow;
tw.WriteElementString("CreatedOnUTC", XmlConvert.ToString(_createdOnUTC));
If you're using a DOM:
Code:
XmlElement elem = myDoc.CreateElement("CreatedOnUTC");
elem.InnerText = XmlConvert.ToString(DateTime.UtcNow);
MyRootElem.AppendChild(elem);

To go the other way, using a XmlTextReader:
Code:
_createdOnUTC = XmlConvert.ToDateTime(tr.ReadElementString("CreatedOnUTC"));
Using a DOM:
Code:
XmlElement elem = myDoc.SelectSingleNode("root/CreatedOnUTC");
if (elem != null)
{
   _createdOnUTC = XmlConvert.ToDateTime(elem.InnerText);
}
else
{
   _createdOnUTC = DateTime.MinValue;
}

Chip H

If you want to get the best response to a question, please check out FAQ222-2244 first
 
Below is the code in my .aspx page that adds a table to an XML file. ActivityLog.xsd is the typed dataset referenced from the ActivityLog.xml file. It includes an attribute labeled "time" with a dateTime datatype. Whenever the code below is executed, the dataset is added to the ActivityLog.xml file in the correct form. However, the "time" attribute contains a (null) value for the data view. Any help would be greatly appreciated.

Code:
Private Sub RecordHit(ByVal UserName As String, ByVal PhotoCnt As Int16, ByVal EstCnt As Int16, _
    ByVal TtlLossCnt As Int16, ByVal OtherCnt As Int16)
        Dim ds As DataSet
        Dim dtbl As DataTable
        Dim sAccntHolder As String
        Dim arrUsers() As DataRow
        Dim drowNew As DataRow
        Dim xCnvrt As System.Xml.XmlConvert

        ds = New DataSet
        ds.ReadXml(ConfigurationSettings.AppSettings.Item("UserAccounts"))

        dtbl = ds.Tables(0)

        '------------------------------------------------------------------
        'Use the user name to get the account holder name
        '------------------------------------------------------------------
        arrUsers = dtbl.Select("name='" & UserName & "'")
        sAccntHolder = arrUsers(0).Item(0)
        ds = Nothing
        dtbl = Nothing

        ds = New DataSet
        ds.ReadXml(ConfigurationSettings.AppSettings.Item("ActivityLog"))

        If ds.Tables.Count = 0 Then
            '------------------------------------------------------------
            'Add a table to accept activity entries
            '------------------------------------------------------------
            With ds
                .Tables.Add("Hit")
                .Tables(0).Columns.Add("user")
                .Tables(0).Columns("user").ColumnMapping = MappingType.Attribute
                .Tables(0).Columns.Add("ip")
                .Tables(0).Columns("ip").ColumnMapping = MappingType.Attribute
                .Tables(0).Columns.Add("time")
                .Tables(0).Columns("time").ColumnMapping = MappingType.Attribute
                .Tables(0).Columns.Add("accountholder")
                .Tables(0).Columns.Add("appraiser")
                .Tables(0).Columns.Add("claim")
                .Tables(0).Columns.Add("policy")
                .Tables(0).Columns.Add("claimant")
                .Tables(0).Columns.Add("photo")
                .Tables(0).Columns.Add("estimate")
                .Tables(0).Columns.Add("totalloss")
                .Tables(0).Columns.Add("other")
            End With
        End If

        dtbl = ds.Tables(0)
        drowNew = dtbl.NewRow
        drowNew("user") = UserName
        drowNew("ip") = Me.Request.UserHostAddress
        drowNew("time") = DateTime.UtcNow
        drowNew("accountholder") = sAccntHolder
        drowNew("appraiser") = Request.QueryString.Item("Appraiser")
        drowNew("claim") = Session.Item("claim")
        drowNew("policy") = Session.Item("policy")
        drowNew("claimant") = Session.Item("claimant")
        drowNew("photo") = PhotoCnt
        drowNew("estimate") = EstCnt
        drowNew("totalloss") = TtlLossCnt
        drowNew("other") = OtherCnt

        dtbl.Rows.Add(drowNew)
        ds.WriteXml(ConfigurationSettings.AppSettings.Item("ActivityLog"))
        ds.Dispose()
        dtbl.Dispose()
    End Sub
 
Sorry, I don't have any experience in using bound data. I always write my own data access code.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top