Link to home
Start Free TrialLog in
Avatar of TRUENEUTRAL
TRUENEUTRAL

asked on

XMLException

Why am I getting this?  It's maddening.

System.Xml.XmlException: The root element is missing.
   at System.Xml.XmlTextReader.Read()
   at System.Xml.XmlReader.MoveToContent()
   at System.Xml.XmlReader.IsStartElement(String localname, String ns)
   at System.Xml.Serialization.TempAssembly.CanRead(Int32 methodIndex, XmlReader xmlReader)
   at System.Xml.Serialization.XmlSerializer.CanDeserialize(XmlReader xmlReader)
   at eConnect80_Serializer.Serializer.My_Deserialize(String xmltext) in C:\Documents and Settings\Jedzida\My Documents\Visual Studio Projects\eConnect_Document_Validator\eConnect80_Serializer\Serializer.vb:line 58

The program that is generating this is very complicated, so I put together a simple sample program to demonstrate the error.

Here is my function:
Public Function My_Deserialize(ByVal xmltext As String) As String

            Try
                  Dim myobj As New MyType
                  Dim ser As New Xml.Serialization.XmlSerializer(GetType(MyType))
                  Dim st As New System.IO.MemoryStream
                  Dim writer As New Xml.XmlTextWriter(st, New System.Text.UTF8Encoding)
                  writer.WriteString(xmltext)
                  st.Position = 0

                  Dim reader As New Xml.XmlTextReader(st)
                  reader.Normalization = True

                  st.Position = 0
                  If ser.CanDeserialize(reader) Then
                        myobj = ser.Deserialize(reader)
                  Else
                        MY_Deserialize = "Can't deserialize"

                  End If

            Catch ex As Exception
                  MY_Deserialize = ex.ToString

            End Try
      End Function


'Here is my classes for the serializer

'the following classes should define a document to look like
'<MyType><InnerClass><TEST>try me</TEST></InnerClass></MyType>

<System.Xml.Serialization.XmlRootAttribute("MyType", [Namespace]:="", IsNullable:=False)> _
Public Class MyType
      <System.Xml.Serialization.XmlElementAttribute("InnerClassType", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
 Public InnerClass() As InnerClassType

End Class

Public Class InnerClassType
      <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
      Public TEST As String

End Class



When I pass it the string:
<MyType><InnerClass><TEST>try me</TEST></InnerClass></MyType>

I get the "root element missing" error.  Please correct me if I am wrong, but <MyType> *is* the root element here.  Does anyone now what this error really means?  

There is no inner exception on this one.
Avatar of AmanBrar
AmanBrar

Hi, i think you should have the following at the root -
<?xml version="1.0" encoding="ISO-8859-1"?>

in any case you may use this to validate your xml -
http://www.ltg.ed.ac.uk/~richard/xml-check.html

hope this helps.
Avatar of TRUENEUTRAL

ASKER

Hmmm...
As you can see from my example, the XML is very simple (and valid)

Regardless of whether I use this
<?xml version='1.0'?><MyType><InnerClass><TEST>try me</TEST></InnerClass></MyType>

or this
<?xml version="1.0" encoding="ISO-8859-1"?><MyType><InnerClass><TEST>try me</TEST></InnerClass></MyType>

I get the same exact error
Avatar of Bob Learned
Which line is 58?  My guess is myobj = ser.Deserialize(reader).  I am going to assume also that you are deserializing an object that you have previously serialized.

What would be interesting to know is how important is this information to the deserialization process:

xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

This is taken from a Micro$oft example:
HOW TO: Serialize and Deserialize XML in Visual Basic .NET
http://support.microsoft.com/kb/316730/EN-US/#3

<?xml version="1.0" encoding="utf-8"?>
<clsProduct xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>Widget</Name>
  <Description>Faster, better, cheaper</Description>
  <Quantity>5</Quantity>
</clsProduct>


Bob
Hey LearnedOne!  Happy Holidays!

Line 58 is
If ser.CanDeserialize(reader) Then

On your suggestion I tried this

<?xml version='1.0'?><MyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><InnerClass><TEST>try me</TEST></InnerClass></MyType>


Still get the same error.


Also, may not necessarily be xml that I have previously serialized.  I am trying to deserialize it and catching the error in order to validate the structure.  It no errors happen, I am deserializing it into an object to do further validation on the values.  If all passes, the original xml document will be sent on to another program to process.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
LearnedOne:

I noticed your example used a hard file rather than a memory stream.  This got me thinking that maybe there was an issue with the reader using a memory stream to deserialize.  Perhaps it was not recognizing the stream as xml?

So I modified the code to the following:

Dim myobj As New MyType
Dim ser As New Xml.Serialization.XmlSerializer(GetType(MyType))
Dim st As New System.IO.MemoryStream
Dim writer As New Xml.XmlTextWriter(st, New System.Text.UTF8Encoding)
writer.WriteString(xmltext)
st.Position = 0

'begin change
Dim context As Xml.XmlParserContext
context = New Xml.XmlParserContext(Nothing, Nothing, "", Xml.XmlSpace.None)
Dim reader As New Xml.XmlTextReader(xmltext, Xml.XmlNodeType.Document, context)
'end change

reader.Normalization = True
st.Position = 0
If ser.CanDeserialize(reader) Then
      myobj = ser.Deserialize(reader)
Else
      MY_Deserialize = "Can't deserialize"
End If


This gives me a way to tell the reader that this is an xml document without having to specify any details about the document.

I seems to work fine this way!

Thanks for your example!  It got me thinking on the right track.
-TN