locked
DateTime format error in XML SOAP Response from web service RRS feed

  • Question

  • User489117894 posted

    There is an error in XML document (4, 294). ---> System.FormatException: String was not recognized as a valid DateTime

    I'm getting an XML DateTime exception on my C# web app client when I call a web service that was auto-generated using Visual Studio web references. The error occurs at the web service client endpoint as the web reference proxy is formatting the XML response. Because of the type of error, I'm think it has to do with RequestTimeStamp or ResponseTimeStamp elements within the XML response. I'm wondering if there is a .NET bug or incompatibility with converting certain Text formats to DateTimes formats. What's a good way to trace the cause of this error and to look at the XML response- I couldn't find a way in Visual Studio 2003 to examine the XML response from the Web Service. I appreciate the help I can get.

    - Henry
     

    Heres the exception details:

    System.InvalidOperationException: There is an error in XML document (4, 294). ---> System.FormatException: String was not recognized as a valid DateTime.
       at System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style, DateTime& result)
       at System.DateTime.ParseExact(String s, String[] formats, IFormatProvider provider, DateTimeStyles style)
       at System.Xml.XmlConvert.ToDateTime(String s, String[] formats)
       at System.Xml.Serialization.XmlCustomFormatter.ToDateTime(String value, String[] formats)
       at System.Xml.Serialization.XmlCustomFormatter.ToDateTime(String value)
       at System.Xml.Serialization.XmlSerializationReader.ToDateTime(String value)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read3_OnDemandMeterPingResponseType(Boolean isNullable, Boolean checkType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read9_requestMeterStatusSyncResponse()
       --- End of inner exception stack trace ---
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
       at EAIInterface.MeterPingService.OnDemandMeterPingService.requestMeterStatusSync(OnDemandMeterPingRequestType onDemandMeterPingRequest) in C:\DATA\Website\Practice\EAIInterface\Web References\MeterPingService\Reference.cs:line 42
       at EAIInterface.EAI.GetMeter(String SPID, String UID) in c:\data\website\practice\eaiinterface\eai.cs:line 766

     

    Date time XML sample from Web Service host: <requestTimeStamp></requestTimeStamp>

    <requestTimeStamp><requestTimeStamp>2007-02-01T16:32:34.948Z</requestTimeStamp>
    <responseTimeStamp>2007-03-05T10:46:39.300-08:00</responseTimeStamp>
    </requestTimeStamp><responseTimeStamp></responseTimeStamp>

    Tuesday, March 6, 2007 9:08 PM

Answers

  • User-512755392 posted

    Hi Henry ,

    Above date format looks good to me.

    I'm wondering if there is a .NET bug or incompatibility with converting certain Text formats to DateTimes formats. What's a good way to trace the cause of this error and to look at the XML response- I couldn't find a way in Visual Studio 2003 to examine the XML response from the Web Service. 

    You can access web service using httpwebrequest to trace the error,here is an example:

     

        public void my_Call_WebService()
        {
    
            HttpWebRequest hwRequest;
            HttpWebResponse hwResponse;
            System.IO.StreamReader sReader;
            XmlDocument xmlDoc = new XmlDocument();
            int iTimeout = 1;
            System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");// Pipes the stream to a higher level stream reader with the required encoding format. 
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            string strXMLRequest = @"&lt;soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
                                      @"<?xml:namespace prefix = soap /><soap:Body>" +
                                      @"<ADD xmlns="http:<span class=" cmt?>//sampletestservice/'>" +
                                      @"&lt;a>3</a>" +
                                      @"&lt;b>4</b>" +
                                      @"&lt;/Add>" +
                                      @"&lt;/soap:Body>" +
                                      @"&lt;/soap:Envelope>";
    
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(strXMLRequest);
    
            string strWebServiceURL = "http://localhost/WS_1/Test.asmx";
            hwRequest = (HttpWebRequest)HttpWebRequest.Create(strWebServiceURL);
            hwRequest.Headers.Add("SOAPAction", "http://sampletestservice/Add");
    
            hwRequest.ContentType = "text/xml; charset=utf-8";
            hwRequest.Accept = "text/xml";
            hwRequest.Credentials = CredentialCache.DefaultCredentials;
            hwRequest.Method = "POST";
            hwRequest.Timeout = iTimeout * 1000;
            //hwRequest.ContentLength = soapEnvelopeXml.OuterXml.Length;; 
            using (Stream stream = hwRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
            hwResponse = (HttpWebResponse)hwRequest.GetResponse();
            sReader = new StreamReader(hwResponse.GetResponseStream());
            string strXMLReply = sReader.ReadToEnd();
            sReader.Close();
            xmlDoc.LoadXml(strXMLReply);
            Response.Write(xmlDoc.OuterXml);
        }
     </soap:Body>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 7, 2007 10:07 PM