1

The data that I am sending to 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 That I am getting The string was not recognized as a valid DateTime. There is a unknown word starting at index 2.

2 Answers2

2

DateTime.Parse will try all kinds of different patterns. Assuming you've got an explicit format in mind, I would use DateTime.ParseExact - that way you know what it's looking for.

And as lassevk's said, look at the string before you try to parse it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Have you debugged it and put a breakpoint there to verify that the string you get is actually the string you expect it to be?

Also, this:

m.Result( "${date}" )

Where is that group defined?

I would split up the extraction of the Regex found value into a string, then convert it. This would allow you to place a breakpoint in there and inspect the value found.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • I am using VS 2003 1.1 Framework. Itried to debug it but its not giving any info. all it showed was "$date". I even assigned to a string like string date=m.Result( "${date}" ) and then tried to look using QuickWatch but it also showed nothing –  Jan 08 '09 at 11:28