2017-02-04 113 views
1

我試圖反序列化一個名爲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=''

+0

異常通常包含文檔中畸形的位置。通常,字節順序標記(BOM)缺失或UTF8內容中存在問題 –

回答

1

你的問題是,如果你試圖序列類型的根對象List<CSounds>你會得到XML這樣的:

<ArrayOfCSound> 
    <CSound> 
    <id>one</id> 
    </CSound> 
</ArrayOfCSound> 

注意根元素<ArrayOfX>在相同的字符串X爲嵌套項目結束元素<X>?其中根元素被命名爲ArrayOfSound但該項目元素被命名爲CSound這是你的示例XML不一致。

要解決這一矛盾,就需要引入一個根類型如下:

[XmlRoot("ArrayOfSounds")] 
public class CSoundsRoot 
{ 
    public CSoundsRoot() { this.Sounds = new List<CSound>(); } 

    [XmlElement("CSound")] 
    public List<CSound> Sounds { get; set; } 
} 

然後做:

XmlSerializer serializer = new XmlSerializer(typeof(CSoundsRoot)) 
var root = (CSoundsRoot)serializer.Deserialize(reader); 
var sounds = root == null ? new List<CSounds>() : root.Sounds; 

順便說一句,在XML序列化的錯誤通常可以diagosed通過序列化您的類型並將觀察到的XML與預期的XML進行比較。

樣品fiddle