| 

.NET C# Java Javascript Exception

1
Hallo zusammen,

in meiner Applikation möchte ich Daten in einem XML file anstelle einer lokalen Client Datenbank abspeichern. Ich habe bereits das Dataset mit dem XML File implementiert und das speichern in das XML File funktioniert auch schon. Da es sich um eine WPF App handelt wird das auslesen über Databindung durchgeführt werden. Nur verstehe ich nicht wofür man Relationen im Dataset definieren kann. Beim auslesen aus dem XML file muss ich doch sowieso selbst die serialisierung durch führen um aus den Daten wieder Objekte zu erstellen, oder nicht?
Irgendwie habe ich die Vermutung das ich das mit den Datasets noch nicht ganz verstanden. Hat da jemand vielleicht einen Hilfreichen Link wo erklärt wird wie man Datasets mit XML files verwenden kann? Ich habe zwar was gefunden aber das ist nicht so verständlich.
News:
17.10.2013
Damien 43 1 6
2 Antworten
0
Beim auslesen aus dem XML file muss ich doch sowieso selbst die serialisierung durch führen um aus den Daten wieder Objekte zu erstellen, oder nicht?

Verwende hierfür doch die bereitgestellten Methoden des DataSets:
DataSet.WriteXml
DataSet.ReadXml
17.10.2013
ffordermaier 8,4k 3 9
0
Hallo Damien,

Ich bin mit C# unterwegs, aber der Code gilt bestimmt auch für VB...
Objekte lassen sich ohne Dataset super einfach in XML umwandeln und wieder laden...

Im Prinzip so:

private static void SaveToDocumentFormat(T serializableObject, Type[] extraTypes, string path, IsolatedStorageFile isolatedStorageFolder) {
using (var textWriter = CreateTextWriter(isolatedStorageFolder, path)) {
var xmlSerializer = CreateXmlSerializer(extraTypes);
xmlSerializer.Serialize(textWriter, serializableObject);
}
}


private static T LoadFromDocumentFormat(Type[] extraTypes, string path, IsolatedStorageFile isolatedStorageFolder) {
T serializableObject;

using (var textReader = CreateTextReader(isolatedStorageFolder, path)) {
var xmlSerializer = CreateXmlSerializer(extraTypes);
serializableObject = xmlSerializer.Deserialize(textReader) as T;
}

return serializableObject;
}


Leider ist die Quelle, von wo ich den Code kopiert habe nicht mehr online. Daher hier der komplette Code dazu:

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace cleverDev.BasicUtils.XmlUtils {
/// <summary>
/// Serialization format types.
/// </summary>
public enum SerializedFormat {
/// <summary>
/// Binary serialization format.
/// </summary>
Binary,

/// <summary>
/// Document serialization format.
/// </summary>
Document
}

/// <summary>
/// Facade to XML serialization and deserialization of strongly typed objects to/from an XML file.
///
/// References: XML Serialization at http://samples.gotdotnet.com/:
/// http://samples.gotdotnet.com/QuickStart/howto/default.aspx?url=/quickstart/howto/doc/xmlserialization/rwobjfromxml.aspx
/// </summary>
public static class XmlSerializerHelper<T> where T : class // Specify that T must be a class.
{
#region Load methods

/// <summary>
/// Loads an object from an XML file in Document format.
/// </summary>
/// <example>
/// <code>
/// serializableObject = ObjectXMLSerializer&lt;SerializableObject&gt;.Load(@"C:\XMLObjects.xml");
/// </code>
/// </example>
/// <param name="path">Path of the file to load the object from.</param>
/// <returns>Object loaded from an XML file in Document format.</returns>
public static T Load(string path) {
T serializableObject = LoadFromDocumentFormat(null, path, null);
return serializableObject;
}

/// <summary>
/// Loads an object from an XML file using a specified serialized format.
/// </summary>
/// <example>
/// <code>
/// serializableObject = ObjectXMLSerializer&lt;SerializableObject&gt;.Load(@"C:\XMLObjects.xml", SerializedFormat.Binary);
/// </code>
/// </example>
/// <param name="path">Path of the file to load the object from.</param>
/// <param name="serializedFormat">XML serialized format used to load the object.</param>
/// <returns>Object loaded from an XML file using the specified serialized format.</returns>
public static T Load(string path, SerializedFormat serializedFormat) {
T serializableObject;

switch (serializedFormat) {
case SerializedFormat.Binary:
serializableObject = LoadFromBinaryFormat(path, null);
break;

default:
serializableObject = LoadFromDocumentFormat(null, path, null);
break;
}

return serializableObject;
}

/// <summary>
/// Loads an object from an XML file in Document format, supplying extra data types to enable deserialization of custom types within the object.
/// </summary>
/// <example>
/// <code>
/// serializableObject = ObjectXMLSerializer&lt;SerializableObject&gt;.Load(@"C:\XMLObjects.xml", new Type[] { typeof(MyCustomType) });
/// </code>
/// </example>
/// <param name="path">Path of the file to load the object from.</param>
/// <param name="extraTypes">Extra data types to enable deserialization of custom types within the object.</param>
/// <returns>Object loaded from an XML file in Document format.</returns>
public static T Load(string path, Type[] extraTypes) {
T serializableObject = LoadFromDocumentFormat(extraTypes, path, null);
return serializableObject;
}

/// <summary>
/// Loads an object from an XML file in Document format, located in a specified isolated storage area.
/// </summary>
/// <example>
/// <code>
/// serializableObject = ObjectXMLSerializer&lt;SerializableObject&gt;.Load("XMLObjects.xml", IsolatedStorageFile.GetUserStoreForAssembly());
/// </code>
/// </example>
/// <param name="fileName">Name of the file in the isolated storage area to load the object from.</param>
/// <param name="isolatedStorageDirectory">Isolated storage area directory containing the XML file to load the object from.</param>
/// <returns>Object loaded from an XML file in Document format located in a specified isolated storage area.</returns>
public static T Load(string fileName, IsolatedStorageFile isolatedStorageDirectory) {
T serializableObject = LoadFromDocumentFormat(null, fileName, isolatedStorageDirectory);
return serializableObject;
}

/// <summary>
/// Loads an object from an XML file located in a specified isolated storage area, using a specified serialized format.
/// </summary>
/// <example>
/// <code>
/// serializableObject = ObjectXMLSerializer&lt;SerializableObject&gt;.Load("XMLObjects.xml", IsolatedStorageFile.GetUserStoreForAssembly(), SerializedFormat.Binary);
/// </code>
/// </example>
/// <param name="fileName">Name of the file in the isolated storage area to load the object from.</param>
/// <param name="isolatedStorageDirectory">Isolated storage area directory containing the XML file to load the object from.</param>
/// <param name="serializedFormat">XML serialized format used to load the object.</param>
/// <returns>Object loaded from an XML file located in a specified isolated storage area, using a specified serialized format.</returns>
public static T Load(string fileName, IsolatedStorageFile isolatedStorageDirectory, SerializedFormat serializedFormat) {
T serializableObject;

switch (serializedFormat) {
case SerializedFormat.Binary:
serializableObject = LoadFromBinaryFormat(fileName, isolatedStorageDirectory);
break;

default:
serializableObject = LoadFromDocumentFormat(null, fileName, isolatedStorageDirectory);
break;
}

return serializableObject;
}

/// <summary>
/// Loads an object from an XML file in Document format, located in a specified isolated storage area, and supplying extra data types to enable deserialization of custom types within the object.
/// </summary>
/// <example>
/// <code>
/// serializableObject = ObjectXMLSerializer&lt;SerializableObject&gt;.Load("XMLObjects.xml", IsolatedStorageFile.GetUserStoreForAssembly(), new Type[] { typeof(MyCustomType) });
/// </code>
/// </example>
/// <param name="fileName">Name of the file in the isolated storage area to load the object from.</param>
/// <param name="isolatedStorageDirectory">Isolated storage area directory containing the XML file to load the object from.</param>
/// <param name="extraTypes">Extra data types to enable deserialization of custom types within the object.</param>
/// <returns>Object loaded from an XML file located in a specified isolated storage area, using a specified serialized format.</returns>
public static T Load(string fileName, IsolatedStorageFile isolatedStorageDirectory, Type[] extraTypes) {
T serializableObject = LoadFromDocumentFormat(null, fileName, isolatedStorageDirectory);
return serializableObject;
}

#endregion

#region Save methods

/// <summary>
/// Saves an object to an XML file in Document format.
/// </summary>
/// <example>
/// <code>
/// SerializableObject serializableObject = new SerializableObject();
///
/// ObjectXMLSerializer&lt;SerializableObject&gt;.Save(serializableObject, @"C:\XMLObjects.xml");
/// </code>
/// </example>
/// <param name="serializableObject">Serializable object to be saved to file.</param>
/// <param name="path">Path of the file to save the object to.</param>
public static void Save(T serializableObject, string path) {
SaveToDocumentFormat(serializableObject, null, path, null);
}

/// <summary>
/// Saves an object to an XML file using a specified serialized format.
/// </summary>
/// <example>
/// <code>
/// SerializableObject serializableObject = new SerializableObject();
///
/// ObjectXMLSerializer&lt;SerializableObject&gt;.Save(serializableObject, @"C:\XMLObjects.xml", SerializedFormat.Binary);
/// </code>
/// </example>
/// <param name="serializableObject">Serializable object to be saved to file.</param>
/// <param name="path">Path of the file to save the object to.</param>
/// <param name="serializedFormat">XML serialized format used to save the object.</param>
public static void Save(T serializableObject, string path, SerializedFormat serializedFormat) {
switch (serializedFormat) {
case SerializedFormat.Binary:
SaveToBinaryFormat(serializableObject, path, null);
break;

default:
SaveToDocumentFormat(serializableObject, null, path, null);
break;
}
}

/// <summary>
/// Saves an object to an XML file in Document format, supplying extra data types to enable serialization of custom types within the object.
/// </summary>
/// <example>
/// <code>
/// SerializableObject serializableObject = new SerializableObject();
///
/// ObjectXMLSerializer&lt;SerializableObject&gt;.Save(serializableObject, @"C:\XMLObjects.xml", new Type[] { typeof(MyCustomType) });
/// </code>
/// </example>
/// <param name="serializableObject">Serializable object to be saved to file.</param>
/// <param name="path">Path of the file to save the object to.</param>
/// <param name="extraTypes">Extra data types to enable serialization of custom types within the object.</param>
public static void Save(T serializableObject, string path, Type[] extraTypes) {
SaveToDocumentFormat(serializableObject, extraTypes, path, null);
}

/// <summary>
/// Saves an object to an XML file in Document format, located in a specified isolated storage area.
/// </summary>
/// <example>
/// <code>
/// SerializableObject serializableObject = new SerializableObject();
///
/// ObjectXMLSerializer&lt;SerializableObject&gt;.Save(serializableObject, "XMLObjects.xml", IsolatedStorageFile.GetUserStoreForAssembly());
/// </code>
/// </example>
/// <param name="serializableObject">Serializable object to be saved to file.</param>
/// <param name="fileName">Name of the file in the isolated storage area to save the object to.</param>
/// <param name="isolatedStorageDirectory">Isolated storage area directory containing the XML file to save the object to.</param>
public static void Save(T serializableObject, string fileName, IsolatedStorageFile isolatedStorageDirectory) {
SaveToDocumentFormat(serializableObject, null, fileName, isolatedStorageDirectory);
}

/// <summary>
/// Saves an object to an XML file located in a specified isolated storage area, using a specified serialized format.
/// </summary>
/// <example>
/// <code>
/// SerializableObject serializableObject = new SerializableObject();
///
/// ObjectXMLSerializer&lt;SerializableObject&gt;.Save(serializableObject, "XMLObjects.xml", IsolatedStorageFile.GetUserStoreForAssembly(), SerializedFormat.Binary);
/// </code>
/// </example>
/// <param name="serializableObject">Serializable object to be saved to file.</param>
/// <param name="fileName">Name of the file in the isolated storage area to save the object to.</param>
/// <param name="isolatedStorageDirectory">Isolated storage area directory containing the XML file to save the object to.</param>
/// <param name="serializedFormat">XML serialized format used to save the object.</param>
public static void Save(T serializableObject, string fileName, IsolatedStorageFile isolatedStorageDirectory, SerializedFormat serializedFormat) {
switch (serializedFormat) {
case SerializedFormat.Binary:
SaveToBinaryFormat(serializableObject, fileName, isolatedStorageDirectory);
break;

default:
SaveToDocumentFormat(serializableObject, null, fileName, isolatedStorageDirectory);
break;
}
}

/// <summary>
/// Saves an object to an XML file in Document format, located in a specified isolated storage area, and supplying extra data types to enable serialization of custom types within the object.
/// </summary>
/// <example>
/// <code>
/// SerializableObject serializableObject = new SerializableObject();
///
/// ObjectXMLSerializer&lt;SerializableObject&gt;.Save(serializableObject, "XMLObjects.xml", IsolatedStorageFile.GetUserStoreForAssembly(), new Type[] { typeof(MyCustomType) });
/// </code>
/// </example>
/// <param name="serializableObject">Serializable object to be saved to file.</param>
/// <param name="fileName">Name of the file in the isolated storage area to save the object to.</param>
/// <param name="isolatedStorageDirectory">Isolated storage area directory containing the XML file to save the object to.</param>
/// <param name="extraTypes">Extra data types to enable serialization of custom types within the object.</param>
public static void Save(T serializableObject, string fileName, IsolatedStorageFile isolatedStorageDirectory, Type[] extraTypes) {
SaveToDocumentFormat(serializableObject, null, fileName, isolatedStorageDirectory);
}

#endregion

#region Private

private static FileStream CreateFileStream(IsolatedStorageFile isolatedStorageFolder, string path) {
FileStream fileStream;

if (isolatedStorageFolder == null) {
fileStream = new FileStream(path, FileMode.OpenOrCreate);
} else {
fileStream = new IsolatedStorageFileStream(path, FileMode.OpenOrCreate, isolatedStorageFolder);
}

return fileStream;
}

private static T LoadFromBinaryFormat(string path, IsolatedStorageFile isolatedStorageFolder) {
T serializableObject;

using (var fileStream = CreateFileStream(isolatedStorageFolder, path)) {
var binaryFormatter = new BinaryFormatter();
serializableObject = binaryFormatter.Deserialize(fileStream) as T;
}

return serializableObject;
}

private static T LoadFromDocumentFormat(Type[] extraTypes, string path, IsolatedStorageFile isolatedStorageFolder) {
T serializableObject;

using (var textReader = CreateTextReader(isolatedStorageFolder, path)) {
var xmlSerializer = CreateXmlSerializer(extraTypes);
serializableObject = xmlSerializer.Deserialize(textReader) as T;
}

return serializableObject;
}

private static TextReader CreateTextReader(IsolatedStorageFile isolatedStorageFolder, string path) {
TextReader textReader;

if (isolatedStorageFolder == null) {
textReader = new StreamReader(path);
} else {
textReader = new StreamReader(new IsolatedStorageFileStream(path, FileMode.Open, isolatedStorageFolder));
}

return textReader;
}

private static TextWriter CreateTextWriter(IsolatedStorageFile isolatedStorageFolder, string path) {
TextWriter textWriter;

if (isolatedStorageFolder == null) {
textWriter = new StreamWriter(path);
} else {
textWriter = new StreamWriter(new IsolatedStorageFileStream(path, FileMode.OpenOrCreate, isolatedStorageFolder));
}

return textWriter;
}

private static XmlSerializer CreateXmlSerializer(Type[] extraTypes) {
var objectType = typeof(T);

XmlSerializer xmlSerializer;

if (extraTypes != null) {
xmlSerializer = new XmlSerializer(objectType, extraTypes);
} else {
xmlSerializer = new XmlSerializer(objectType);
}

return xmlSerializer;
}

private static void SaveToDocumentFormat(T serializableObject, Type[] extraTypes, string path, IsolatedStorageFile isolatedStorageFolder) {
using (var textWriter = CreateTextWriter(isolatedStorageFolder, path)) {
var xmlSerializer = CreateXmlSerializer(extraTypes);
xmlSerializer.Serialize(textWriter, serializableObject);
}
}

private static void SaveToBinaryFormat(T serializableObject, string path, IsolatedStorageFile isolatedStorageFolder) {
using (var fileStream = CreateFileStream(isolatedStorageFolder, path)) {
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, serializableObject);
}
}

#endregion
}
}


Viele Grüße,
Markus
18.10.2013
MaHop 71 4

Stelle deine .net-Frage jetzt!