Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c# - Error in XML document (2,2)

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...