में एक शब्दकोश
XmlDocument xmlDoc = new XmlDocument();
// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("TireStudy");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
foreach (var saveItem in _SaveItems)
{
XPathNavigator nav = rootNode.CreateNavigator();
using (var writer = nav.AppendChild())
{
var serializer = new XmlSerializer(saveItem.Value.GetType());
writer.WriteWhitespace("");
serializer.Serialize(writer, saveItem.Value);
writer.Close();
}
}
xmlDoc.Save(fileName);
यह दिनचर्या काम करता है इस प्रकार है, लेकिन मैं शब्दकोश की कुंजी मान के रूप में अच्छी तरह से फ़ाइल में सहेजा जा करने के लिए चाहते हैं और मुझे यकीन है कि कैसे को डी-क्रमानुसार करने नहीं कर रहा हूँ यह फ़ाइल बनाता है क्योंकि मैं उन्हें पढ़ने से पहले वस्तुओं के प्रकारों को नहीं जानता।
भाग 2 (मैं एक प्रश्न के नए भागों को जोड़ने से नफरत है, लेकिन मैं समस्याओं आगे जाने को संबोधित करने के लिए एक बेहतर तरीका नहीं दिख रहा है)
मैं अब निम्नलिखित कोड है,
var knownTypes = new List<Type>
{
typeof(ObservableCollection<string>),
typeof(ObservableCollection<Segments>),
typeof(Segments),
typeof(List<string>)
};
var serialized = _SaveItems.Serialize(knownTypes);
लेकिन मैं निम्न अपवाद
Type 'System.Collections.Generic.List`1[System.String]' cannot be added to list of known types since another type 'System.Collections.ObjectModel.ObservableCollection`1[System.String]' with the same data contract name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfstring' is already present. If there are different collections of a particular type - for example, List<Test> and Test[], they cannot both be added as known types. Consider specifying only one of these types for addition to the known types list.
मिल रहा है, तो या तो typeof (ObservableCollection) या typeof (सूची) यह एक मैं नष्ट कर दिया की जरूरत शिकायत हटाना यह अपवाद नहीं।
भाग 2 के बारे में, मैं नहीं यकीन अगर यह एक ढांचा [बाधा] (http://msdn.microsoft.com/en-us/library/aa347850.aspx) या [बग] (http://support.microsoft.com/kb/967339)। – xmamat
किसी भी तरह से, मैंने अपनी सूची से छुटकारा पा लिया और मेरी कक्षा में ऑब्जर्जेबल कोलेक्शन का उपयोग किया और जीवन आसान हो गया। इस पर मदद के लिए आपका धन्यवाद। –
PlTaylor