Error in XML document (2,2)

c#, xml-serialization

Solution

You will have to look at the base exception to find out the problem. The exception caught probably contains 4 or more inner exceptions.

EG:

try
{
  ...
}
catch (Exception ex)
{
  Console.WriteLine(ex.GetBaseException());
}

Problem

I have some xml files and I am trying to deserialize as below in the given code. ``` using (StreamReader srFileContent = new StreamReader(filePath)) { XmlSerializer serializer = new XmlSerializer(typeof(messageType)); messageType objMessage = (messageType)serializer.Deserialize(srFileContent); } ``` Here file locate at filePath does not contains the following lines ``` <?xml version="1.0"?> <message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> ``` and thats why I'm getting the error. Can u help me how to add this lines runtime before deserialize the stream of given file. Error is given below: System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: was not expected. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadermessageType.‌​Read161_message() --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) at CCR2BB.frmMain.BWConvertProcess_DoWork()

Original source