C# XML 文档中的错误 (2,2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10737892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:03:14  来源:igfitidea点击:

Error in XML document (2,2)

c#xml-serialization

提问by Hiren Visavadiya

I have some xml files and I am trying to deserialize as below in the given code.

我有一些 xml 文件,我试图在给定的代码中如下反序列化。

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

这里 filePath 的 file locate 不包含以下几行

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

System.InvalidOperationException: XML 文档 (2, 2) 中存在错误。---> System.InvalidOperationException: 不是预期的。在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadermessageType.??Read161_message() --- 内部异常堆栈跟踪结束 --- 在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) 在 System .Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) 在 CCR2BB.frmMain.BWConvertProcess_DoWork()

采纳答案by leppie

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

您必须查看基本异常才能找出问题所在。捕获的异常可能包含 4 个或更多内部异常。

EG:

例如:

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

回答by csteinmueller

The solution in another question was:

另一个问题的解决方案是:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "message";
// xRoot.Namespace = "http://www.cpandl.com";
xRoot.IsNullable = true;

XmlSerializer xs = new XmlSerializer(typeof(messageType),xRoot);

Maybe that's an approach for your problem. Because MSDN is down for my network I can't provide any more documentation for XmlRootAttribute.

也许这是解决您问题的方法。因为我的网络的 MSDN 已关闭,所以我无法为 XmlRootAttribute 提供更多文档。

回答by Swapnil Patil

From where did you get the class 'messageType'?

您从何处获得“messageType”类?

If you have created xsd for your xml using xsd.exe and then using xsd you created this class.

如果您使用 xsd.exe 为 xml 创建了 xsd,然后使用 xsd 创建了此类。

Then your project will have two files which contain this class.

那么您的项目将有两个包含此类的文件。

  • One of the file is designer.cswhich contains this class which is derived from DataSetclass
  • One file is simply a .csfile, which has a partial class.
  • When you use this class while deserializing, it will refer to the class from designer.cs which is derived from DataSet.
  • But if you remove designer.cs from you project, your code will refer to the partial class from .cs file.
  • 其中一个文件designer.cs包含从DataSet类派生的此类
  • 一个文件只是一个.cs文件,它有一个部分类。
  • 当您在反序列化时使用这个类时,它将引用从DataSet.
  • 但是,如果您从项目中删除 Designer.cs,您的代码将引用 .cs 文件中的部分类。

By removing this designer.cs file, which had the class derived from DataSet, I was able to resolve this error.

通过删除这个具有从 派生的类的designer.cs 文件,DataSet我能够解决这个错误。