|
-
Jul 31st, 2006, 09:46 AM
#1
Thread Starter
Frenzied Member
[2005] XML deserialization
I'm using this code to try and load an xmlDocument into a serializable object
VB Code:
Public Shared Function LoadObjectFromXmlDoc(ByVal x As System.Xml.XmlDocument, ByVal objType As Type) As Object
Try
Dim tmpObj As Object
Dim oXS As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(objType)
Dim oStm As New System.IO.MemoryStream()
x.Save(oStm)
tmpObj = oXS.Deserialize(oStm)
oStm.Close()
Return tmpObj
Catch ex As Exception
Return Nothing
End Try
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>
-
Jul 31st, 2006, 10:03 AM
#2
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.
-
Jul 31st, 2006, 10:21 AM
#3
Thread Starter
Frenzied Member
Re: [2005] XML deserialization
That didn't fix it. I'm getting the same error. Any other ideas?
-
Jul 31st, 2006, 11:44 AM
#4
Thread Starter
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|