c# - Xamarin: Deserializing XML - "There is an error in XML document" -
in order use xml files in xamarin-forms project, i'm trying recreate steps given in example code , error message:
an exception of type 'system.invalidoperationexception' occurred in system.xml.xmlserializer.dll not handled in user code
additional information: there error in xml document (2, 2).
the example code works fine way.
this xml file use (as embedded resource):
<?xml version="1.0" encoding="utf-8" ?> <items> <item> <name>one</name> <state>alpha</state> </item> <item> <name>two</name> <state>two</state> </item> </items>
this c# code use:
using system; using xamarin.forms; using system.reflection; using system.io; using system.xml.serialization; using system.collections.generic; namespace xmltestproject { public class xmlcontentpage : contentpage { public xmlcontentpage() { //get access xml file var assembly = gettype().gettypeinfo().assembly; stream stream = assembly.getmanifestresourcestream("xmltestproject.xmlfile.xml"); list<item> items; using (var reader = new system.io.streamreader(stream)) { var serializer = new xmlserializer(typeof(list<item>)); items = (list<item>)serializer.deserialize(reader); } var listview = new listview(); listview.itemssource = items; content = new stacklayout { children = { listview } }; } } public class item { public string name { get; set; } public string state { get; set; } public override string tostring() { return name; } } }
i'm using visual studio 2015 community edition , xamarin.forms 2.2.0.5-pre2
the xml should have arrayofitem instead of items :
<?xml version="1.0" encoding="utf-8" ?> <arrayofitem> <item> <name>one</name> <state>alpha</state> </item> <item> <name>two</name> <state>two</state> </item> </arrayofitem>
Comments
Post a Comment