how to deserialize a xml stream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • conniehl
    New Member
    • Apr 2008
    • 3

    how to deserialize a xml stream

    I am able to deserialize a local XML file by passing a textReader object to the xmlSerializer.D eserialize method.

    My problem Iis that I need to pass a XML file path in form of a URL instead of a local file. I use a webRequest class to read the file, code as below:

    // Create a 'WebRequest' object with the specified url.
    WebRequest myWebRequest = WebRequest.Crea te(xmlPath);
    // Send the 'WebRequest' and wait for response.
    WebResponse myWebResponse = myWebRequest.Ge tResponse();
    // Obtain a 'Stream' object associated with the response object.
    Stream ReceiveStream = myWebResponse.G etResponseStrea m();
    System.Text.Enc oding encode = System.Text.Enc oding.GetEncodi ng("utf-8");
    // Pipe the stream to a higher level stream reader with the required encoding format.
    StreamReader readStream = new StreamReader(Re ceiveStream, encode);
    XmlSerializer serializer = new XmlSerializer(t ypeof(bookCatal og));
    myBookCatalog = (bookCatalog)se rializer.Deseri alize(ReceiveSt ream);

    However I am getting an error "There is an error in XML document (0, 0)." on the last line. Does anyone know what the problem could be?

    Thanks in advance for any help.
  • blackjack2150
    New Member
    • Feb 2007
    • 79

    #2
    Originally posted by conniehl
    I am able to deserialize a local XML file by passing a textReader object to the xmlSerializer.D eserialize method.

    My problem Iis that I need to pass a XML file path in form of a URL instead of a local file. I use a webRequest class to read the file, code as below:

    // Create a 'WebRequest' object with the specified url.
    WebRequest myWebRequest = WebRequest.Crea te(xmlPath);
    // Send the 'WebRequest' and wait for response.
    WebResponse myWebResponse = myWebRequest.Ge tResponse();
    // Obtain a 'Stream' object associated with the response object.
    Stream ReceiveStream = myWebResponse.G etResponseStrea m();
    System.Text.Enc oding encode = System.Text.Enc oding.GetEncodi ng("utf-8");
    // Pipe the stream to a higher level stream reader with the required encoding format.
    StreamReader readStream = new StreamReader(Re ceiveStream, encode);
    XmlSerializer serializer = new XmlSerializer(t ypeof(bookCatal og));
    myBookCatalog = (bookCatalog)se rializer.Deseri alize(ReceiveSt ream);

    However I am getting an error "There is an error in XML document (0, 0)." on the last line. Does anyone know what the problem could be?

    Thanks in advance for any help.
    I'm not 100%, but I think that

    ReceiveStream.P osition = 0;

    will do the trick. Put it just before the last line.

    Comment

    • conniehl
      New Member
      • Apr 2008
      • 3

      #3
      Originally posted by blackjack2150
      I'm not 100%, but I think that

      ReceiveStream.P osition = 0;

      will do the trick. Put it just before the last line.

      That does the trick. Thank you!!

      Comment

      Working...