Xmlserializer.deserialize();说“XML文档中存在错误(2,2)” [英] Xmlserializer.deserialize(); says "there is an error in XML document (2, 2)"
问题描述
大家好,
我正在尝试从xml文件反序列化数据,而xml遵循xsd文件。我还生成了一个包含xsd文件类的C#文件。
此代码工作正常:
Hi guys,
I'm trying to deserialize data from a xml file, and the xml follows an xsd file. I also generated a C# file containing classes from the xsd file.
This code works fine:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, xsdfile);
XmlReader reader = XmlReader.Create(xmlfile, settings);
while(reader.Read()); // just to see if an exception occurs
所以它让我认为xml根据xsd文件是有效的。 (而且,我肯定知道这是有效的)
但当我更换这一行时:
So it makes me think the xml is valid according to the xsd file. (Moreover, I know for sure that it is valid)
But when I replace this line:
while(reader.Read());
这条线:
With this line:
Dump dump = (Dump)serializer.Deserialize(reader);
我得到这个非托管异常:
I get this non managed exception:
There is an error in XML document (2, 2).
---> System.InvalidOperationException: <Dump xmlns=''> wasn't expected
但是没有
But there is no
<Dump xmlns=''>
在我的xml中,只有
<Dump>
。
所以我有点迷茫..你能帮忙吗? ?
谢谢!
我的尝试:
搜索到目前为止没有任何帮助:/
.
So I'm a bit lost.. Can you help?
Thanks !
What I have tried:
Searches but nothing helped so far :/
推荐答案
尝试将以下注释添加到班级的顶部,记住xml区分大小写
Try to add the following annotations to the top of your class, remember xml is case sensitive
[Serializable, XmlRoot("Dump")]
public partial class Dump
{
}
这篇关于Xmlserializer.deserialize();说“XML文档中存在错误(2,2)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!