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.
LVL 5
TRUENEUTRALAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AmanBrarCommented:
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.
0
TRUENEUTRALAuthor Commented:
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
0
Bob LearnedCommented:
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
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

TRUENEUTRALAuthor Commented:
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.
0
Bob LearnedCommented:
This is the test program that I came up with and it worked fine without error:

  Private Sub TestSerialization()

    SerializeObject()

    TestDeserializeObject()

  End Sub


  Private Sub SerializeObject()

    Dim test As TestObject = New TestObject

    Dim ser As XmlSerializer = New XmlSerializer(GetType(TestObject))

    Dim writer As New Xml.XmlTextWriter("C:\Temp\Test.txt", New System.Text.UTF8Encoding)

    ser.Serialize(writer, test)

    writer.Close()

  End Sub


  Private Sub TestDeserializeObject()

    Dim st As StreamReader = New StreamReader("C:\Temp\Test.txt")

    Dim ser As XmlSerializer = New XmlSerializer(GetType(TestObject))

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

    If ser.CanDeserialize(reader) Then
      Debug.WriteLine("Yes")
    Else
      Debug.WriteLine("No")
    End If

  End Sub


==============================================
Output:
<?xml version="1.0" encoding="utf-8"?><TestObject xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Name>Bob</Name><Address>Dayton, Ohio</Address></TestObject>

Class:
<Serializable()> Public Class TestObject

  Public Name As String = "Bob"
  Public Address As String = "Dayton, Ohio"

End Class


Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TRUENEUTRALAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.