简体   繁体   中英

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

I have the following xml:

XML

I have the following C# class

C# GlobalClass

I am trying to convert the xml content into C# custom object like that:

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. 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.

From MSDN

The name of the XML root element that is generated and recognized in an XML-document instance. The default is the name of the serialized class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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