\r\n

Unknown DateTime Exception C #

The data I am sending to the convertStringToDataSet function is


<NewDataSet>\r\n  <Table ID="Table1">
                \r\n
                <PATNAME>Doe,JaneN</PATNAME>\r\n
                <LEVEL>175</LEVEL>\r\n
                <WHEN>2007-09-13T23:00:00.0000000-05:00</WHEN>\r\n
                <NOTE>New 180-day low -- ALERT: loss of 11.6 % in 123 days</NOTE>\r\n
                <IS_BAD>32</IS_BAD>\r\n
                <NAME>202a</NAME>\r\n
                <ID>2459</ID>\r\n
                <LASTNAME>Doe</LASTNAME>\r\n
                <FIRSTNAME>Jane</FIRSTNAME>\r\n
                <MIDDLENAME>N</MIDDLENAME>\r\n
            </Table>\r\n  <Table ID="Table2">
                \r\n
                <PATNAME>Face,SmileyE</PATNAME>\r\n
                <LEVEL>124</LEVEL>\r\n
                <WHEN>2007-10-16T23:00:00.0000000-05:00</WHEN>\r\n
                <NOTE>New 180-day low -- ALERT: loss of 14.5 % in 86 days</NOTE>\r\n
                <IS_BAD>32</IS_BAD>\r\n
                <NAME>101b</NAME>\r\n
                <ID>2736</ID>\r\n
                <LASTNAME>Face</LASTNAME>\r\n
                <FIRSTNAME>Smiley</FIRSTNAME>\r\n
                <MIDDLENAME>E</MIDDLENAME>\r\n
            </Table>\r\n</NewDataSet>

      



private DataSet convertStringToDataSet(string xmlString)
        {
            // Search for datetime values of the format 
            // --> 2004-08-22T00:00:00.0000000-05:00
            string rp = @"(?\d{4}-\d{2}-\d{2})(?T\d{2}:\d{2}:\d{2}.\d{7}-)(?\d{2})(?:\d{2})";
            // Replace UTC offset value

            string fixedString = Regex.Replace( xmlString, rp, new MatchEvaluator( getHourOffset ) );

            DataSet dataSet = new DataSet();
            StringReader stringReader = new StringReader( fixedString );
            dataSet.ReadXml( stringReader );

            return dataSet;
        }

        private static string getHourOffset( Match m )
        {
            // Need to also account for Daylights Savings 
            // Time when calculating UTC offset value
            string Date=m.Result( "${date}" );
            DateTime dtLocal = DateTime.Parse( m.Result( "${date}" ) );
            DateTime dtUTC = dtLocal.ToUniversalTime();
            int hourLocalOffset = dtUTC.Hour - dtLocal.Hour;
            int hourServer = int.Parse( m.Result( "${hour}" ) );
            string newHour = ( hourServer + ( hourLocalOffset - 
                hourServer ) ).ToString( "0#" );
            string retString = m.Result( "${date}" + "${time}" +
                newHour + "${last}" );

            return retString;
        }

      

The exception I am getting The string was not recognized as a valid DateTime. There is an unknown word starting at index 2.

+1


source to share


2 answers


DateTime.Parse will use all sorts of templates. Assuming you have an explicit format, I would use DateTime.ParseExact - that way you know what it's looking for.



And as lassevk said, look at the line before trying to parse it.

+2


source


Have you debugged it and put a breakpoint there to make sure the line you are getting is actually the line you expect?

Moreover, it is:

m.Result( "${date}" )

      



Where is this group defined?

I would strip the extraction of the found Regex value into a string and then convert it. This will allow you to place a breakpoint there and check the value found.

0


source







All Articles