2011-07-04 12 views
8

सूची में एक्सएमएल रिवर्स किसी को भी पता है कि कैसे मैं (या अगर यह संभव नहीं है) एक्सएमएल मैं नीचेको क्रमानुसार सूची <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 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> 

अब, गुगलिंग के कुछ घंटों के बाद और कोशिश कर रहा हूं कि मैं अटक गया हूं (सबसे अधिक संभावना है कि मेरे दिमाग में पहले से ही कुछ छुट्टी है)। क्या कोई मेरी मदद कर सकता है कि इस एक्सएमएल को वापस सूची में कैसे बदला जाए?

धन्यवाद

+0

नई XmlSerializer (o.GetType()) deserialize (.. ।) –

+0

क्या आपको वास्तव में एक कस्टम शब्दकोश की आवश्यकता है? जेनेरिक शब्दकोश में कुंजी और मान के रूप में कोई भी प्रकार हो सकता है। –

उत्तर

14

बस इसे deserialize:

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); 
संबंधित मुद्दे