错误的XML文档中(2,2) [英] Error in XML document (2,2)
问题描述
我有一些XML文件,我想在给定的代码如下反序列化。
使用(StreamReader的srFileContent =新的StreamReader(文件路径))
{
XmlSerializer的序列化=新的XmlSerializer(typeof运算(为messageType));
为messageType objMessage =(为messageType)serializer.Deserialize(srFileContent);
}
下面文件中找到的文件路径不包含以下行
<?XML版本=1.0>?;
<消息的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance
的xmlns:XSD =http://www.w3.org/2001/XMLSchema >
和这就是为什么我得到的错误。 。u能帮助我如何添加此行运行之前反序列化给定文件的流
错误是如下:
System.InvalidOperationException:有是在XML文档
中的一个错误(2,2)。 ---> System.InvalidOperationException:在
预计不会。在
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadermessageType. Read161_message()
---内部异常堆栈跟踪的结尾---在System.Xml.Serialization.XmlSerializer.Deserialize(的XmlReader
XmlReader中,字符串encodingStyle的,在CCR2BB.frmMain.BWConvertProcess_DoWork()
System.Xml.Serialization.XmlSerializer.Deserialize(TextReader的
的TextReader) BLOCKQUOTE>
解决方案您将不得不看基本异常,找出问题。 。或许抓住了异常包含4个或更多内部异常
例如:
试
{
:
}
赶上(异常前)
{
Console.WriteLine(ex.GetBaseException());
}
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()
解决方案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()); }
这篇关于错误的XML文档中(2,2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!