Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
320 views
Welcome To Ask or Share your Answers For Others

1 Answer

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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...