Results 1 to 4 of 4

Thread: [2005] XML deserialization

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    [2005] XML deserialization

    I'm using this code to try and load an xmlDocument into a serializable object
    VB Code:
    1. Public Shared Function LoadObjectFromXmlDoc(ByVal x As System.Xml.XmlDocument, ByVal objType As Type) As Object
    2.         Try
    3.             Dim tmpObj As Object
    4.             Dim oXS As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(objType)
    5.             Dim oStm As New System.IO.MemoryStream()
    6.  
    7.             x.Save(oStm)
    8.             tmpObj = oXS.Deserialize(oStm)
    9.             oStm.Close()
    10.             Return tmpObj
    11.         Catch ex As Exception
    12.             Return Nothing
    13.         End Try
    14.     End Function

    But I keep getting an error saying "There is an error in XML document (0, 0).". The inner exception is "{"Root element is missing."}". But that's not true. It is a valid xml document. I serialize a class to create this document. This is a sample of what the xml file looks like when I save it to a file.

    Code:
    <?xml version="1.0" encoding="utf-16" ?> 
    - <ArrayOfSearchResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <SearchResults>
      <Show>Dream Team</Show> 
      <Url>http://www.tv.com/dream-team/show/5430/summary.html?full_summary=1&tag=showspace_links;full_summary</Url> 
      <Number>0</Number> 
      </SearchResults>
    - <SearchResults>
      <Show>Time Team</Show> 
      <Url>http://www.tv.com/time-team/show/21725/summary.html?full_summary=1&tag=showspace_links;full_summary</Url> 
      <Number>0</Number> 
      </SearchResults>
    </ArrayOfSearchResults>
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] XML deserialization

    I get the same error when I deserialize. The problem is with the encoding="utf-16". I don't know why this is a problem or what causes it, but for some reason, my computer can't read them. If you change it to utf-8, then it will work.

    When you serialize it into an XML string, just do a replace for the utf-16 to utf-8 and everything will work fine:

    sXML = SerializeFunction(<args>);
    sXML = sXML.Replace("utf-16", "utf-8")

    I realize that this is a hack and there's probably some other way to get your machine to be able to read XML files with the utf-16 encoding or to get your serializtion to use the utf-8 encoding, but I wasn't able to find them and this way works.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] XML deserialization

    That didn't fix it. I'm getting the same error. Any other ideas?
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] XML deserialization

    I figured out what the issue was. The memory stream was at the last last byte when I called deserialize. So if I set the position to 0, then it works. I did also have to change the encoding to utf-8, like Tom said. does anyone know why that is? Also when serializing an object is there a way to control that, because as Tom said the way he suggested is a hack.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width