I'm trying to deserialize an XML file to an object in my android application, developed with Xamarin Monodroid).
I generate my XML in pure C#, copy the XML file in the assets folder of the application, then deserialize it when the app starts.
[Serializable]
public class MyGreatObject
{
public string ObjectName;
public string ObjectLink;
public string ObjectStuff;
public List<OtherGreatObject> OtherObjects;
public MyGreatObject()
{
OtherObjects = new List<OtherGreatObject>();
}
}
The OtherGreaterObject
has common attributes (string format I mean). So, I generate the XML and have something like this:
<?xml version="1.0" encoding="utf-8"?>
<MyGreatObject xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ObjectName>This is the object name</ObjectName>
<ObjectLink>http://www.xamarin-is-awesome.com</ObjectLink>
<ObjectStuff>Description</ObjectStuff>
<OtherObjects>
<OtherGreatObject>...</OtherGreatObject>
<OtherGreatObject>...</OtherGreatObject>
</OtherObjects>
</MyGreatObject>
And I try to deserialize it like that:
internal static class SerializableXml
{
public static MyGreatObject LoadData(Context context_, string fileName_)
{
MyGreatObject greatObject = new MyGreatObject();
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyGreatObject));
using (Stream input = context_.Assets.Open(fileName_, Android.Content.Res.Access.Buffer))
greatObject = xmlSerializer.Deserialize(input) as MyGreatObject;
}
catch (Exception exception)
{
Android.Util.Log.Error("XML", "ERROR IN LOADING");
Android.Util.Log.Error("XML", exception.ToString());
throw;
}
return greatObject;
}
}
But I get the error System.Xml.XmlException: Document element did not appear. Line 1, position 1.
System.Xml.XmlException: Document element did not appear. Line 1, position 1.
.
at Mono.Xml2.XmlTextReader.Read() [0x00000] in <filename unknown>:0
at System.Xml.XmlTextReader.Read() [0x00000] in <filename unknown>:0
at System.Xml.XmlReader.MoveToCOntent() [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot() [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializer.Deserialize(System.Xml.SerializationReader reader) [0x00000] in <filename unknown>:0
I lost plenty of time on Google, so I'm asking you.
Thanks a lot in advance for the help you can provide.
Well, problem resolved.
Google didn't find anything about the error (in my situation). I thought that the XmlSerializer
(I read about an error in Mono Framework few years ago), but it wasn't.
Than, I thought that something was wrong in the XML, but wasn't sure at all. Why would I have such a error message ? I should have "Invalid node name", "Invalid attribute", or something like that.
But I was right. A node had a bad name , and this returns Document element did not appear. Line 1, position 1.
Document element did not appear. Line 1, position 1.
(or the exception message that means nothing at all) !
So, problem solved.
I came across this error as well using Xamarin.iOS 8.4 Even though on Android everything worked fine, on the iOS build I received the error:
Document element did not appear. Line 1, position 1.
my XML file (which was not written manually, but serialized in c#) had the following line:
<?xml version="1.0" ?>
I fixed it by adding the encoding property as follows:
<?xml version="1.0" encoding="utf-8"?>
If you have many files, clearly this is not the best solution, but perhaps there is a method in the serializer that edits the encoding. I did not look into that since I did not need to, but if anyone comes across this issue, it may be a start.
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.