2012-12-21 66 views
1

我正在使用DataContractSerializer反序列化具有多個集合的Xml。 的XML看起來像下面使用DataContractSerializer反序列化具有多個集合的Xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<collectionContents> 
    <collection> 
     <name>Folder1</name> 
     <id>33399446</id> 
     <contents>TestFolder1</contents> 
    </collection> 
    <collection> 
     <name>Folder2</name> 
     <id>566494</id> 
     <contents>TestFolder2</contents> 
    </collection> 
    <file> 
     <name>2012-08-24_171456.jpg</name> 
     <id>33399624</id> 
     <size>47612</size> 
     <lastModified>2012-12-18T18:54:50.000-08:00</lastModified> 
    </file> 
    <file> 
     <name>1033.ico</name> 
     <id>33412726</id> 
     <size>23246</size> 
     <lastModified>2012-12-18T20:37:15.000-08:00</lastModified> 
    </file> 
</collectionContents> 

我創建DataContractSerializer的反序列化的XML。

[DataContract(Namespace = "")] 
class Item 
{ 
    [DataMember(Name = "name")] 
    public string name; 
    [DataMember(Name = "id")] 
    public string id; 
} 

[DataContract(Name = "collection", Namespace = "")] 
class Folder : Item 
{ 
    [DataMember(Name = "contents")] 
    public string contents; 
} 

[DataContract(Name = "file", Namespace = "")] 
class File : Item 
{ 
    [DataMember(Name = "size")] 
    public long size; 
    [DataMember(Name = "lastModified", Order = 3)] 
    public string lastModified; 
} 

[CollectionDataContract(Name = "collectionContents", Namespace = "")] 
class Folders : List<Folder> 
{} 

[CollectionDataContract(Name = "collectionContents", Namespace = "")] 
class Files : List<File> 
{} 

我可以用兩個的DataContractSerializer拿到兩個集合folers &文件。

DataContractSerializer folderSer = new DataContractSerializer(typeof(Folderes)); 
DataContractSerializer fileSer = new DataContractSerializer(typeof(Files)); 

但需要反序列化兩次, 沒有人知道,有什麼辦法只使用一個DataContractSerializer的拿到兩個集?

下面我有累,但它返回的文件夾/文件數爲0

[DataContract(Name = "collectionContents", Namespace = "")] 
class ItemCollectionList 
{ 
    [DataMember(Name = "collection")] 
    public Folders folders;  // Return count is 0; 
    [DataMember(Name = "file")] 
    public Files files;   // Return count is 0; 
} 
+0

您不需要在帖子中包含簽名 - 您的用戶卡會自動添加。閱讀[常見問題](http://stackoverflow.com/faq#signatures)瞭解更多詳情。 – Artemix

回答

0

你能包裹在你的XMLcollectionfile元素與foldersfiles元素? (以便它更好地匹配您的ItemCollectionList類型)

相關問題