简体   繁体   English

反序列化时XML文档中存在错误(C#)

[英]There is an error in XML document when deserializing(C#)

I have the following xml: 我有以下xml:

XML XML格式

I have the following C# class 我有以下C#类

C# GlobalClass C#GlobalClass

I am trying to convert the xml content into C# custom object like that: 我正在尝试将xml内容转换为C#自定义对象,如下所示:

string xmlFilePath = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/Settings4/settings.xml";
XmlSerializer deserializer = new XmlSerializer(typeof(GlobalClass));
TextReader textReader = new StreamReader(xmlFilePath);
GlobalClass globalVariables;
globalVariables = (GlobalClass)deserializer.Deserialize(textReader);
textReader.Close();

But I get 但是我明白了

There is an error in XML document 

on the line of code 在代码行上

globalVariables = (GlobalClass)deserializer.Deserialize(textReader);

I make GlobalClass inherit from Application because I want GlobalClass to be global that is to say I want to use its properties throughout all activities. 我使GlobalClass从Application继承,因为我希望GlobalClass是全局的,也就是说我想在所有活动中使用其属性。 What I'm doing wrong to recieve that error? 收到该错误我做错了什么?

In your class put something like this [Serializable, XmlRoot("YourRoot")] When you work with deserialization it might not found. 在您的类中放入以下内容: [Serializable, XmlRoot("YourRoot")] 反序列化时 ,可能找不到。

From MSDN MSDN

The name of the XML root element that is generated and recognized in an XML-document instance. 在XML文档实例中生成和识别的XML根元素的名称。 The default is the name of the serialized class. 默认值为序列化类的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM