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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…