我試圖反序列化一個名爲CSound的反序列化XML在Xamarin
public class CSound
{
public string id { get; set; }
public string name { get; set; }
public string file { get; set; }
public string fav { get; set; }
}
..from這個XML
<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfSounds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CSound>
<id>1</id>
<name>SampleName1</name>
<file>SampleFileName1</file>
<fav>true</fav>
</CSound>
<CSound>
<id>2</id>
<name>SampleName2</name>
<file>SampleFileName2</file>
<fav>false</fav>
</CSound>
<CSound>
<id>3</id>
<name>SampleName3</name>
<file>SampleFileName3</file>
<fav>true</fav>
</CSound>
<CSound>
<id>4</id>
<name>SampleName4</name>
<file>SampleFileName4</file>
<fav>false</fav>
</CSound>
<CSound>
<id>5</id>
<name>SampleName5</name>
<file>SampleFileName5</file>
<fav>true</fav>
</CSound>
</ArrayOfSounds>
T'm嘗試使用這部分代碼進行反序列化類
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("SaynarSounds.Resources.XML.Sounds.xml");
List<CSound> sounds;
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<CSound>));
sounds = (List<CSound>)serializer.Deserialize(reader);
}
if (sounds == null)
{
sounds = new List<CSound>();
}
return sounds;
找到資源並開始加載,但在反序列化中出現此錯誤:
System.InvalidOperationException:有是XML文檔中的一個錯誤。
你能幫我嗎?
UPDATE:
異常拋出這個消息
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidOperationException: There is an error in XML document. ---> System.InvalidOperationException: was not expected 02-04 12:29:58.045 E/mono-rt (3046): at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x0005b] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:182 02-04 12:29:58.045 E/mono-rt (3046): at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot() [0x00028] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87 02-04 12:29:58.045 E/mono-rt (3046): at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x0001c] in /Users/builder/data/lanes/3511/501e63ce/source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:364
但我沒有一個xmlns=''
。
異常通常包含文檔中畸形的位置。通常,字節順序標記(BOM)缺失或UTF8內容中存在問題 –