English 中文(简体)
XML 文档中的错误(2,2)
原标题:Error in XML document (2,2)

我有一些xml文件,我正试图在给定代码中进行以下的消毒。

using (StreamReader srFileContent = new StreamReader(filePath))  
{
    XmlSerializer serializer = new XmlSerializer(typeof(messageType));   
    messageType objMessage = (messageType)serializer.Deserialize(srFileContent);  
}

在此文件定位于文件Path 文件不包含以下行

<?xml version="1.0"?>
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

这就是为什么我得到错误的原因。可以帮助我如何在解密给定文件流之前添加此行运行时间 。

以下给出错误 :

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

最佳回答

您需要查看基本例外以找出问题。 所捕捉的例外可能包含4个或4个以上的内部例外 。

埃格:

try
{
  ...
}
catch (Exception ex)
{
  Console.WriteLine(ex.GetBaseException());
}
问题回答

另一个问题的解决办法是:

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

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

也许这是解决你问题的方法。因为 MSDN 已经关闭了我的网络, 我无法为 XmlRoootAttrimitte 提供更多文件 。

你从哪里得到班级信息 Type?

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

然后您的工程将有两个包含此类的文档 。

  • One of the file is designer.cs which contains this class which is derived from DataSet class
  • One file is simply a .cs file, 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.

通过删除这个由 DataSet 衍生的类别。 cs 文件, 我解决了这个错误 。

我认为您必须声明 类 < code> XmlRoootAttrimitte 的 < desco> messesageType 。 例如,

[System.Xml.Serialization.XmlRootAttribute(ElementName = "message", IsNulable=false)]

public class messageType 
{
...
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签