2013-05-14 37 views
2

我覺得我要去了。我已經寫了一百個反序列化例程,但是這個正在殺死我!似乎無法反序列化在c#中使用XmlSerializer非常簡單的xml#

以下是我從服務中返回的內容。一串非常簡單的字符串......我想。

<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <string>Action &amp; Adventure</string> 
    <string>Comedy</string> 
    <string>Drama</string> 
    <string>Family</string> 
    <string>Horror</string> 
    <string>Independent &amp; World</string> 
    <string>Romance</string> 
    <string>Sci-Fi/Fantasy</string> 
    <string>Thriller &amp; Crime</string> 
</ArrayOfstring> 

我使用開箱反序列化

var serializer = new XmlSerializer(typeof(List<string>)); 
var reader = new StringReader(xmlString); 
var GenreList = (List<string>)serializer.Deserialize(reader); 

,但我得到的反序列化線以下錯誤:

<ArrayOfstring xmlns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'> was not expected 

我曾嘗試包括命名空間和創造的一切方式試圖讓這個工作的異國情調的對象。瘋狂的時間量。最後,我用JSON請求了它,並用Json.net反序列化了它。

但是我很好奇我做錯了什麼!

回答

4

XML序列化程序無法反序列化一個簡單類型或簡單類型的,無需額外規格列表,但DataContractReader可以:

 string content = @" 
     <ArrayOfstring xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""> 
      <string>Action &amp; Adventure</string> 
      <string>Comedy</string> 
      <string>Drama</string> 
      <string>Family</string> 
      <string>Horror</string> 
      <string>Independent &amp; World</string> 
      <string>Romance</string> 
      <string>Sci-Fi/Fantasy</string> 
      <string>Thriller &amp; Crime</string> 
     </ArrayOfstring>"; 

     var serializer = new DataContractSerializer(typeof(string[])); 
     var reader = new XmlTextReader(new StringReader(content)); 
     var GenreList = new List<string>((string[])serializer.ReadObject(reader)); 
+0

-1'XML序列化程序無法反序列化一個簡單類型或者我不會講那麼肯定簡單types'的列表。 (順便說一句:我的downvote只是針對未來讀者的虛假陳述,如果你刪除它並僅解釋DataContractReader,我將撤消我的downvote) – I4V

+0

更新了陳述。在不添加根元素的情況下,指定名稱空間或其他「技巧」不會。 – jessehouwing

+0

哪裏可以找到DataContractSerializer? – ecoe

0

你也可以使用一個簡單的類來達到同樣的效果。請注意,爲了簡潔,我從XML文件中刪除了命名空間。如果您願意,您可以在序列化程序中實現對命名空間的讀取。

public class ArrayOfstring 
    { 
     [XmlElement("string")] 
     public List<string> strings; 
    } 

    private void Deserialize(string xmlString) 
    { 
     var serializer = new XmlSerializer(typeof(ArrayOfstring)); 
     var reader = new StringReader(xmlString); 
     var GenreList = ((ArrayOfstring) serializer.Deserialize(reader)).strings; 
    } 
+0

我試過了,但仍然失敗。 [XmlRoot] public class ArrayOfstring {0mlElement(「string」)] public string [] Strings {get;組; } } – NER1808

3

當然XmlSerializer可以反序列化它。所有你需要的是創建XmlSerializer的如下

var serializer = new XmlSerializer(typeof(List<string>), 
            new XmlRootAttribute() { ElementName = "ArrayOfstring", Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays" }); 
0

這將工作

DataContractSerializer xmlSer = new DataContractSerializer(typeof(string[])); 
TextReader reader=new StreamReader(xmlString); 
var stringArr= (string[])xmlSer.ReadObject(reader); 
List<string> listStr=new List<>(); 
for(var s in stringArr) 
{ 
listStr.Add(s); 
} 
相關問題