2011-07-04 7 views
8

を一覧表示するXMLを逆に誰もが知っているどのようにI(またはそれをすることができますならば)私はシリアライズリストXMLに<T>、および<T>

[Serializable()] 
public class CustomDictionary 
{ 
    public string Key { get; set; } 
    public string Value { get; set; } 
} 

public class OtherClass 
{ 
    protected void BtnSaveClick(object sender, EventArgs e) 
    { 
     var analysisList = new List<CustomDictionary>(); 

     // Here i fill the analysisList with some data 
     // ... 

     // This renders the xml posted below 
     string myXML = Serialize(analysisList).ToString(); 
     xmlLiteral.Text = myXML; 
    } 

    public static StringWriter Serialize(object o) 
    { 
     var xs = new XmlSerializer(o.GetType()); 
     var xml = new StringWriter(); 
     xs.Serialize(xml, o); 

     return xml; 
    } 
} 

の下に作成していXMLを逆にレンダリングされたXML

<?xml version="1.0" encoding="utf-16"?> 
<ArrayOfCustomDictionary xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <CustomDictionary> 
    <Key>Gender</Key> 
    <Value>0</Value> 
    </CustomDictionary> 
    <CustomDictionary> 
    <Key>Height</Key> 
    <Value>4</Value> 
    </CustomDictionary> 
    <CustomDictionary> 
    <Key>Age</Key> 
    <Value>2</Value> 
    </CustomDictionary> 
</ArrayOfCustomDictionary> 

グーグルで試してみると数時間後に私は立ち往生しています(ほとんどの場合、私の脳はすでに休暇を取っている可能性が高いです)。誰もこのXMLをリストに戻す方法を私に助けてくれますか?

おかげ

+0

新しいXmlSerializerを(o.GetType())(デシリアライズ.. 。) –

+0

本当にカスタム辞書が必要ですか?汎用辞書は、キーと値として任意の型を持つことができます。 –

答えて

14

はちょうどそれをデシリアライズ:

public static T Deserialize<T>(string xml) { 
    var xs = new XmlSerializer(typeof(T)); 
    return (T)xs.Deserialize(new StringReader(xml)); 
} 

はこのようにそれを使用します。

var deserializedDictionaries = Deserialize<List<CustomDictionary>>(myXML); 
関連する問題